Skip to content

Instantly share code, notes, and snippets.

@yuu-ito
Last active December 6, 2016 11:28
Show Gist options
  • Save yuu-ito/0bd25268ac46a00ae6a5e69134e34b0d to your computer and use it in GitHub Desktop.
Save yuu-ito/0bd25268ac46a00ae6a5e69134e34b0d to your computer and use it in GitHub Desktop.
[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)
@yuu-ito
Copy link
Author

yuu-ito commented Nov 1, 2016

pythonの関数引き数は参照渡しで、
配列は1つの配列オブジェクトを参照するように渡されてる
--> 別々に突っ込んでも同じとこにはいる。
--> 引き数で渡さないことで同じ配列はみないようになった?

@yuu-ito
Copy link
Author

yuu-ito commented Nov 2, 2016

重要な警告: デフォルト値は 1 度だけしか評価されません。

http://docs.python.jp/2.5/tut/node6.html#SECTION006710000000000000000

@yuu-ito
Copy link
Author

yuu-ito commented Nov 2, 2016

[y-ito@workspace ~]$ pylint test.py | head -n30
No config file found, using default configuration
************* Module test
C: 10, 0: Unnecessary parens after 'return' keyword (superfluous-parens)
C: 28, 0: Unnecessary parens after 'print' keyword (superfluous-parens)
C: 29, 0: Unnecessary parens after 'print' keyword (superfluous-parens)
C:  1, 0: Missing module docstring (missing-docstring)
C:  1, 0: Missing class docstring (missing-docstring)
C:  1, 0: Old-style class defined. (old-style-class)
W:  2, 4: Dangerous default value [] as argument (dangerous-default-value)
C:  6, 4: Invalid argument name "c" (invalid-name)
C:  6, 4: Missing method docstring (missing-docstring)
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment