Re: [問題] 新手請益

看板Python作者 (阿真)時間15年前 (2009/03/20 15:22), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串7/16 (看更多)
看完一系列的討論 找了一些資料 整理了一下 --以下是is的用法-- 先找找Python org~ 2.x版的 (3.x版的寫的也差不多) http://docs.python.org/library/stdtypes.html#boolean-operations-and-or-not 裡面的解釋是: (網頁裡的其中一行...|||) is object identity 就是說 is 這個comparision是問 例如: x is y == x跟y是不是同一個object object的觀念我還在努力 並不是很熟 感覺是跟pointer很像(但pointer我也不熟 囧) 但我猜測 上面的例子 'e ' 跟 e 想必是不同的object 於是我google "python pointer" 裡面的一篇: http://mail.python.org/pipermail/python-list/2003-March/192973.html 提到: Actually, everything in Python is "pointer to something real". This is called "object reference" in Python. 所以'e '也許被放到不同的地方去了(pointer to something real) 也就跟e這個變數(裡面放'e ')所放的位置不同 所以是false 並在shell裡的測試 過程: >>> def func(a): return a >>> a=func >>> b=func >>> a is b True >>> a is func True >>> a <function func at 0x011A6DB0> >>> b <function func at 0x011A6DB0> >>> func <function func at 0x011A6DB0> 可以看到a 跟 b 跟func 是放在同個地方 (在python裡應該是指同一個"object"吧?) --以上是is的用法討論-- ※ 引述《Tiberius (渴望平凡的幸福)》之銘言: : ※ 引述《justlink (('(oo)'))》之銘言: : : 版本 3.1a : : 使用 win32 idle : : e = "e " : : print(e is "e ") : : 得到True : 這是 "e " 被自動 intern 了,所以兩者是「同一個物件」 : : Shell 底下 : : False : : 囧rz 這是? : 這是沒有被 intern 的情形 : 請參考內建函式 intern() 的說明,一般來說「不要假設字串會被自動 intern」 : 比對字串內容是否相符的時候,請用 == 對於以上 我接著做了測試: >>> intern(a('e ')) 'e ' >>> print intern(a('e ')) is 'e ' False 似乎在手動intern之下 還是跟'e '所在的位置不同 也就是之前提到的 Actually, everything in Python is "pointer to something real". This is called "object reference" in Python. 不知道是我在用python 2.4.1版的問題還是其他 所以我覺得是否是在3.1a版的改變?!(我沒抓來測) 不過3.x版的參考手冊又寫的跟2.x版的觀念差不多(我看起來是一樣) 也就是所謂的object identity 我也對3.1版 測試的True 感覺怪怪的 希望有版友可以一起討論、幫忙解答 : : 另外關於 : : round(2.5) : : 這個問題的答案為何是2? : python 3.0 的 round() 改成 round-to-even -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.63.199
文章代碼(AID): #19mqGWE8 (Python)
討論串 (同標題文章)
文章代碼(AID): #19mqGWE8 (Python)