作者klao ( 完。整)
看板Python
標題[問題] 新手f-string問題
時間Fri Jun 18 13:19:25 2021
大家好
小弟新手
最近看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