1. List-related
One interesting question is mystuff. append ('hello'). How does Python explain it. Here is a clear description of the paragraph, so I won't translate it. I will post it directly.
- Python sees you mentionedMystuffAnd looks up that variable. It might have to look backwards to see if you created=, Look and see if it is a function argument, or maybe it's a global variable. Either way it has to findMystuffFirst.
- Once it findsMystuffIt then hits.(Period) operator and starts to lookVariablesThat are a partMystuff. SinceMystuffIs a list, it knows thatMystuffHas a bunch of functions.
- It then hitsAppendAnd compares the name "APPEND" to all the ones thatMystuffSays it owns. If append is in there (it is) then it grabsThatTo use.
- Next Python sees((Parenthesis) and realizes, "Oh hey, this shocould be a function." At this point itCILS(Aka runs, executes) The function just like normally, but instead it callthe function withExtraArgument.
- ThatExtraArgument 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 isMystuff. 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)
This error is very strange. The above explanation explains the cause. But I still don't quite understand it. It may be different from Java.
Paste the following code:
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. Dictionary
It is equivalent to map. Adds and deletes elements to and from a dictionary.
stuff = {'name': 'Zed', 'age': 36, 'height': 6*12+2}stuff['city'] = "San Francisco"stuff[1] = "Wow"del stuff['city']del stuff[1]
You can obtain an element in either of the following ways:
Print stuff ['name'] print stuff ['age'] A = stuff. Get ('texas ', none) # in the above method, if the keyword does not exist, a raise exception occurs. This method is safer. If the keyword 'texas 'does not exist, assign a value to none.
Ordered dictionary, collection.
3. modules, classes, and instances
In fact, modules and classes are similar to dictionaries. See the following:
# dict stylemystuff['apples']# module stylemystuff.apples()print mystuff.tangerine# class stylething = MyStuff()thing.apples()print thing.tangerine
Python running process:
- Python looksMystuff ()And sees that it isClassYou 've defined.
- Python crafts an empty object with all the functions you 've specified in the class usingDef.
- Python then looks to see if you made a "magic"_ Init __Function, and if you have it cballs that function to initialize your newly created empty object.
- InMystuffFunction_ Init __I then get this extra variableSelfWhich is that empty object Python made for me, and I can set variables on it just like you wowould with a module, dict, or other object.
- In this case, I setSelf. TangerineTo a song lyric and then I 've initialized this object.
- Now python can take this newly minted object, and assign it toThingVariable for me to work.