Python 板


LINE

大家好 小弟新手 最近看O'relly自學python 做到一個練習題想請教大家 >>> class Laser: def does(self): return 'disintegrate' >>> class Claw: def does(self): return'crush' >>> class SmartPhone: def does(self): return 'ring' >>> class Robot: def __init__(self): self.Laser=Laser() self.Claw=Claw() self.SmartPhone=SmartPhone() def does(self): return ''' My laser, to %s. My claw, to %s. My smartphone, to %s.'''% ( self.Laser.does(), self.Claw.does(), self.SmartPhone.does() ) >>> robbie = Robot() >>> print(robbie.does()) 結果是 My laser, to disintegrate. My claw, to crush. My smartphone, to ring. 請問我把它改成f-string後,最後面寫成 >>> def does(self): return f''' I have many functions. My laser can {self.Laser.does()} My claw can {self.Claw.does()} My smartphone can {self.SmartPhone.does()} ''' 卻出現 My laser can <__main__.Laser object at 0x000001D36C7C7A30> My claw can <__main__.Claw object at 0x000001D36C7C7A90> My smartphone can <__main__.SmartPhone object at 0x000001D36C7C7310> 想請教大家,問題是出在哪裡? 在此先謝謝了! --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.22.165.64 (臺灣)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/Python/M.1623993567.A.C0A.html
1F:→ Pieteacher: f’{self.xxx}’ 06/18 13:47
2F:→ klao: 請問是改成f'''...{self.Laser.does}...'''這樣寫法嗎?還是 06/18 15:02
3F:→ klao: 會出現一樣的情況 06/18 15:02
4F:推 lycantrope: 是不是忘記call self.xxx.does() ? 至少3.8沒問題 06/18 16:35
5F:推 s0914714: 看起來應該沒問題 你試看看print(self.Laser.does()) 06/18 16:38
6F:→ klao: 回4樓 這樣是可以的 會出現'disintegrate' 06/18 16:40
7F:推 s0914714: Python版本? 06/18 16:45
8F:推 lycantrope: 至少要大於>=3.6 才有f"" 06/18 16:48
9F:推 Conjuror: 樓上正解 06/19 09:04
10F:→ klao: 謝謝各位大大的建議!我的版本是3.9 06/19 21:24
11F:→ alvinlin: f-string裡面放的是變數不是函式 06/20 01:01
12F:推 s0914714: f-string是runtime evaluation 放function是沒問題的 06/20 03:24
13F:推 andy19960407: 一切都是物件 06/20 14:53
14F:推 ddavid: 我測這個沒有發生問題啊 06/21 11:41
15F:推 ddavid: 而且原po那個結果很明顯是直接印到了 self.Laser 這些物件 06/21 11:43
16F:→ ddavid: 本身而沒有印到 self.Laser.does() 的傳回值 06/21 11:44
17F:→ ddavid: 印得出那個東西,首先就排除了版本不具備f-string的問題 06/21 11:45
18F:→ ddavid: 其次,如果是少了括號打成 self.Laser.does,輸出會是 06/21 11:46
19F:→ ddavid: My laser can <bound method Laser.does of <__main__.Las 06/21 11:46
20F:→ ddavid: er object at 0x000001F0BA383DF0>> 06/21 11:46
21F:→ ddavid: 印到的東西會很明確說他印到了Laser裡面的does這個method 06/21 11:47
22F:→ ddavid: ,所以也可以排除 06/21 11:47
23F:→ ddavid: 那個結果完全就是印到 self.Laser 本身,但原Po又打出來說 06/21 11:47
24F:→ ddavid: 他的Code就是 {self.Laser.does()} ,我重現不出來這個錯 06/21 11:48
25F:→ ddavid: 我只能懷疑要嘛其實實際Code是 {self.Laser} ,不然另一個 06/21 11:49
26F:→ ddavid: 可能是其他class的does()不知為何內部改成return self 06/21 11:50
27F:→ dlhk3: 試試看把每個does的return都改成f string 06/22 08:46
28F:→ klao: 謝謝上面兩位大大的講解!手邊現在沒電腦,後天來試試看! 06/24 04:26
29F:推 kane111: 印到記憶體位置了,但code本身看起來沒問題應該會回傳字 06/25 01:25
30F:→ kane111: 串,怪哉 06/25 01:25
31F:→ klao: 今天把python更新到3.9.5,莫名其妙就可以了。還是十分感謝 06/26 04:54
32F:→ klao: 各位先進的講解! 06/26 04:54
33F:推 s0914714: 原先版本是3.9.幾? 06/26 06:55
34F:→ ddavid: 可是我測的時候只用3.8.10也是沒問題 06/26 20:33
35F:→ klao: 原先是3.9.0,但真的跟上面某些留言講的一樣,實在不懂為什 06/29 02:14
36F:→ klao: 麼本來不行 06/29 02:14
37F:推 s0914714: 3.9.0我測試也沒問題 所以可能原本的環境出了啥問題 06/29 07:49







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:BuyTogether站內搜尋

TOP