[問題] 用class包裝後return不到想要的值
目前因為興趣在自學,基礎不是很好。(以前沒程式經驗)
近期在做爬蟲的練習,
有重複賦值的需求,但發現用class包裝後,
卻只會拿到第一次的結果。
(雖然用別的方法可以解決)
但我自己本身想不通為什麼會這樣,也不知道該打什麼關鍵字去澄清這個。
所以想請各位前輩幫忙看看我的觀念哪裡錯誤了。
程式碼如下:
a="a"
b="b"
class TEST():
def __init__(self,test1,test2):
self.test1=test1
self.test2=test2
def test():
test1 = a
test2 = b
return TEST(test1,test2)
test=test()
print (test.test1)
print (test.test2)
a="A"
b="B"
print (test.test1)
print (test.test2)
###Output###
a
b
a
b
############
以上。
結果不是想像中的,
a
b
A
B
是不是我哪裡有天大的錯誤認知阿,
我需要重新複習哪一塊呢?
感謝各位。
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.138.126.141
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1525838401.A.9D7.html
推
05/09 12:10,
6年前
, 1F
05/09 12:10, 1F
→
05/09 12:10,
6年前
, 2F
05/09 12:10, 2F
→
05/09 12:10,
6年前
, 3F
05/09 12:10, 3F
→
05/09 12:12,
6年前
, 4F
05/09 12:12, 4F
→
05/09 12:13,
6年前
, 5F
05/09 12:13, 5F
推
05/09 12:17,
6年前
, 6F
05/09 12:17, 6F
→
05/09 12:17,
6年前
, 7F
05/09 12:17, 7F
→
05/09 12:33,
6年前
, 8F
05/09 12:33, 8F
→
05/10 10:11,
6年前
, 9F
05/10 10:11, 9F
→
05/10 10:12,
6年前
, 10F
05/10 10:12, 10F
→
05/10 10:13,
6年前
, 11F
05/10 10:13, 11F