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不太一样。
贴下代码:
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: ", stuff print "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 style mystuff['apples'] # module style mystuff.apples() print mystuff.tangerine # class style thing = 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.