Review Python BASICS (9)

Source: Internet
Author: User

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.

  1. 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.
  2. Once it findsMystuffIt then hits.(Period) operator and starts to lookVariablesThat are a partMystuff. SinceMystuffIs a list, it knows thatMystuffHas a bunch of functions.
  3. 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.
  4. 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.
  5. 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:

  1. Python looksMystuff ()And sees that it isClassYou 've defined.
  2. Python crafts an empty object with all the functions you 've specified in the class usingDef.
  3. 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.
  4. 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.
  5. In this case, I setSelf. TangerineTo a song lyric and then I 've initialized this object.
  6. Now python can take this newly minted object, and assign it toThingVariable for me to work.

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.