[Python] How to pack and unpack variables in Python?

The follow code snippets show how to use variables smartly.

class MyClass():
    def pack(self,**kwargs):
        for key in kwargs:
            print key +"=>"+ kwargs[key];
    def unpack(self,a,b):
        print a
        print b

if __name__ == '__main__':
    my = MyClass()
    my.pack(name="developer",skill="java")
    p = (1,2)
    p = [3,4]
    my.unpack(*p)
    p = {'a':5,'b':6}
    my.unpack(**p)


發佈了270 篇原創文章 · 獲贊 3 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章