2023年4月6日 星期四
2022年9月30日 星期五
2022年9月26日 星期一
2020年7月8日 星期三
2020年6月24日 星期三
2020年6月3日 星期三
2019年9月25日 星期三
In Python 3, m = [i] vs m[0] = i
在Python 3裡, assign是call by value? 還是call by reference呢?
Python自創了一個名詞,說是call by assignment.
但是從C語言學起的我,還是用call by value or call by reference來說明,會比較清楚。
如果i is integer, then:
case I:
m = [i]
case II:
m[0] = i
Q: 這兩者差在哪呢?
A:
case I:
m = [i], 是call by value, 也就是把m指向[i]位子的value。
case II:
m[0] = i, 是call by reference, 也就是把m[0]位子的值,設成i.
也許有人會覺得,出來的值都一樣,那有啥差別呢?
請看以下code:
#會印出:
byRef
m= [0]
['good']
m= ['good']
['afternoon']
byValue
m= [0]
['fine.']
m= [0]
['thanks']
byValue1
m= 0
1
m= 0
1
byValue2
m= 0
2
m= 0
2
Python自創了一個名詞,說是call by assignment.
但是從C語言學起的我,還是用call by value or call by reference來說明,會比較清楚。
如果i is integer, then:
case I:
m = [i]
case II:
m[0] = i
Q: 這兩者差在哪呢?
A:
case I:
m = [i], 是call by value, 也就是把m指向[i]位子的value。
case II:
m[0] = i, 是call by reference, 也就是把m[0]位子的值,設成i.
也許有人會覺得,出來的值都一樣,那有啥差別呢?
請看以下code:
class Solution: def byValue(self, t, m = [0]): print('m=', m) for i in t: m = [i] return m def byRef(self, t, m = [0]): print('m=', m) for i in t: m[0] = i return m def byValue1(self, t, m = 0): print('m=', m) for i in t: m = 1 return m def byValue2(self, t, m = 0): print('m=', m) for i in t: m = 2 return m def main(): a = Solution() print('byRef') print(a.byRef(['good'])) print(a.byRef(['afternoon'])) print('byValue') print(a.byValue(['fine.'])) print(a.byValue(['thanks'])) print('byValue1') print(a.byValue1(['good'])) print(a.byValue1(['afternoon'])) print('byValue2') print(a.byValue2(['fine.'])) print(a.byValue2(['thanks'])) if __name__ == "__main__": main()
#會印出:
byRef
m= [0]
['good']
m= ['good']
['afternoon']
byValue
m= [0]
['fine.']
m= [0]
['thanks']
byValue1
m= 0
1
m= 0
1
byValue2
m= 0
2
m= 0
2
訂閱:
文章 (Atom)
Lean 4 極簡教學 (Using VS Code on macOS)
1. Install Lean Environment: https://lean-lang.org/install/ 試著完Step one, two, three 2. 在VS code下,建一新的project: 取名Hello_World 3. 嘗試執行Main.lea...
-
之前安裝photoshop CS2常常不能破解, 今天總算安裝成功, 原來是忘了執行"crack.exe"。 詳細step as follows: 1.安裝photoshop cs2,在安裝過程中,必須"一直按下一步", 不能改目錄位置,也...
-
今天中午,跟老姐一起看一公升的眼淚DVD, 後來老媽也加入來看, 看完之後,覺得還不錯, 劇中維持日劇一貫的風格,場景不多, 音樂非常優美,出場的人物不多, 主要圍繞在亞也、亞也他媽媽和醫生、同學間, 一公升的眼淚,是在1986年出的,現在也有中譯版了, 有興趣的網友可以去 博客...
-
關羽跟張飛都掛了 劉備找孔明訴苦 劉備:「雲長魂歸故里,如今三弟也溘然長逝,真乃大悲啊」 孔明:「甜度?」 劉備:「大悲無言」 孔明:「我是問甜度,不是加不加鹽…」




















