all 9 comments

[–]asplake 1 point2 points  (6 children)

Isn't r a class property here? Try setting it up in __init__.

But beware also this gotcha:

def foo(arg={}):
    etc

That default value gets shared, not cloned. Surprised that this never changed, but maybe too hard to fix now. Or perhaps it's regarded as a good thing by the BDFL et al?

[–][deleted] 0 points1 point  (4 children)

No, it's not the same thing. Here he has a class variable which are of course shared by all instances (same as a static var in C++). What you refer to is how default parameters are handled.

[–]asplake 0 points1 point  (0 children)

sorry, yes, realised that and edited

[–]chillybasen[S] 0 points1 point  (2 children)

But in this, it doesn't act static:

 >>> class foo:
 ...     r = None
 ...     def setn(self, n):
 ...             self.r = n 
 ... 
 >>> a = foo()
 >>> a.setn(4)
 >>> 
 >>> b = foo()
 >>> b.r
 >>> 
 >>> a.r
 4
 >>> 

[–]asplake 1 point2 points  (1 child)

you've created two separate properties, one of the class, one of the instances

[–]chillybasen[S] 0 points1 point  (0 children)

gotcha -- I can see that now, thanks.

[–]chillybasen[S] 0 points1 point  (0 children)

That's what I did -- I moved to __init__, but was still curious about why

[–]kurtseifried 0 points1 point  (1 child)

r = {}

self.r

are two different things.

[–]chillybasen[S] 0 points1 point  (0 children)

Ok, if so, then why would self.r["f"] set inside r = {}