[y-ito@workspace ~]$ cat test.py
class Order():
def __init__(self, name, campaigns=[]):
self.name = name
self.campaigns = campaigns
def add(self,c):
self.campaigns.append(c)
def info(self):
return(self.name + ": " + ",".join([c.name for c in self.campaigns]))
class Campaign():
def __init__(self,name):
self.name = name
c1 = Campaign("c1")
c2 = Campaign("c2")
c3 = Campaign("c3")
o1 = Order("o1")
o1.add(c1)
o1.add(c2)
o2 = Order("o2")
o2.add(c3)
print(o1.info()) # c1,c2 って出てほしい
print(o2.info()) # c3 って出てほしい
[y-ito@workspace ~]$ ~/jupyter/env/bin/python test.py
o1: c1,c2,c3 # why?
o2: c1,c2,c3 #
[y-ito@workspace ~]$ ~/jupyter/env/bin/python test2.py
o1: c1,c2
o2: c3
[y-ito@workspace ~]$ git diff test.py test2.py
diff --git a/test.py b/test2.py
index 4a4a6c3..1556cd4 100644
--- a/test.py
+++ b/test2.py
@@ -1,7 +1,7 @@
class Order():
- def __init__(self, name, campaigns=[]):
+ def __init__(self, name):
self.name = name
- self.campaigns = campaigns
+ self.campaigns = []
def add(self,c):
self.campaigns.append(c)
pythonの関数引き数は参照渡しで、
配列は1つの配列オブジェクトを参照するように渡されてる
--> 別々に突っ込んでも同じとこにはいる。
--> 引き数で渡さないことで同じ配列はみないようになった?