1. list相關的
有一個比較有趣的問題,就是mystuff.append('hello'),python是怎麼解釋的。這裡有段話講的比較清楚,就不翻譯了,直接貼過來。
- Python sees you mentioned mystuff and looks up that variable. It might have to look backwards to see if you created with =, look and see if it is a function argument, or maybe it's a global variable. Either way it has to find the mystuff first.
- Once it finds mystuff it then hits the . (period) operator and starts to look at variables that are a part of mystuff. Sincemystuff is a list, it knows that mystuff has a bunch of functions.
- It then hits append and compares the name "append" to all the ones that mystuff says it owns. If append is in there (it is) then it grabs that to use.
- Next Python sees the ( (parenthesis) and realizes, "Oh hey, this should be a function." At this point it calls (aka runs, executes) the function just like normally, but instead it calls the function with an extra argument.
- That extra argument is ... mystuff! I know, weird right? But that's how Python works so it's best to just remember it and assume that's alright. What happens then, at the end of all this is a function call that looks like: append(mystuff, 'hello')instead of what you read which is mystuff.append('hello').
>>> class Thing(object):... def test(hi):... print "hi"...>>> a = Thing()>>> a.test("hello")Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: test() takes exactly 1 argument (2 given)
這個錯誤就很奇怪,上面的解釋就解釋了這個原因。但是還是不太明白,可能和java不太一樣。
貼下代碼:
View Code
ten_things = "Apples Oranges Crows Telephone Light Sugar"print "Wait there's not 10 things in that list, let's fix that."stuff = ten_things.split(' ')more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]while len(stuff) != 10: next_one = more_stuff.pop() print "Adding: ", next_one stuff.append(next_one) print "There's %d items now." % len(stuff)print "There we go: ", stuffprint "Let's do some things with stuff."print stuff[1]print stuff[-1]print stuff.pop()print " ".join(stuff)print "#".join(stuff[3:5])
2. 字典
相當於map。向一個字典中添加和刪除元素。
stuff = {'name': 'Zed', 'age': 36, 'height': 6*12+2}stuff['city'] = "San Francisco"stuff[1] = "Wow"del stuff['city']del stuff[1]
擷取元素,有兩種方式
print stuff['name']print stuff['age']a = stuff.get('Texas', None) #用上面那種方式,如果關鍵字不存在,會raise異常。用這種方式比較保險,如果關鍵字不存在'Texas'不存在,那麼給a賦值為None。
有序的字典,collection。
3. 模組、類、執行個體
其實模組和類都和字典類似。看下面:
# dict stylemystuff['apples']# module stylemystuff.apples()print mystuff.tangerine# class stylething = MyStuff()thing.apples()print thing.tangerine
類的方式python運行過程:
- Python looks for MyStuff() and sees that it is a class you've defined.
- Python crafts an empty object with all the functions you've specified in the class using def.
- Python then looks to see if you made a "magic" __init__ function, and if you have it calls that function to initialize your newly created empty object.
- In the MyStuff function __init__ I then get this extra variable self which is that empty object Python made for me, and I can set variables on it just like you would with a module, dict, or other object.
- In this case, I set self.tangerine to a song lyric and then I've initialized this object.
- Now Python can take this newly minted object, and assign it to the thing variable for me to work with.