重新梳理Python基礎(9)

來源:互聯網
上載者:User

1. list相關的

有一個比較有趣的問題,就是mystuff.append('hello'),python是怎麼解釋的。這裡有段話講的比較清楚,就不翻譯了,直接貼過來。

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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運行過程:

  1. Python looks for MyStuff() and sees that it is a class you've defined.
  2. Python crafts an empty object with all the functions you've specified in the class using def.
  3. 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.
  4. 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.
  5. In this case, I set self.tangerine to a song lyric and then I've initialized this object.
  6. Now Python can take this newly minted object, and assign it to the thing variable for me to work with.

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.