Inheriting dictionary attributes and manipulate them in subclasses

2009-04-17 Thread Dominik Ruf
Hi,

I just stumbled upon the following behaviour.
 class base():
...   dic = {'1':'1', '2':'2'}
...
 class child1(base):
...   def __init__(self):
... self.dic.update({'1':'2'})
...
 class child2(base):
...   pass
...
 c1 = child1()
 c2 = child2()

 print c1.dic
{'1': '2', '2': '2'}
 print c2.dic
{'1': '2', '2': '2'}

This is not what I have excepted.
Although I know the solution to get what I want...
 class base():
...   def __init__(self):
... self.dic = {'1':'1', '2':'2'}
...
 class child1(base):
...   def __init__(self):
... base.__init__(self)
... self.dic.update({'1':'2'})
...
 class child2(base):
...   pass
...
 c1 = child1()
 c2 = child2()

 print c1.dic
{'1': '2', '2': '2'}
 print c2.dic
{'1': '1', '2': '2'}

... I wonder if there is a special reason for the behaviour in the
first example.
Shouldn't the first example behave like the second?

cheers
Dominik
--
http://mail.python.org/mailman/listinfo/python-list


Re: Inheriting dictionary attributes and manipulate them in subclasses

2009-04-17 Thread Diez B. Roggisch
Dominik Ruf wrote:

 Hi,
 
 I just stumbled upon the following behaviour.
 class base():
 ...   dic = {'1':'1', '2':'2'}
 ...
 class child1(base):
 ...   def __init__(self):
 ... self.dic.update({'1':'2'})
 ...
 class child2(base):
 ...   pass
 ...
 c1 = child1()
 c2 = child2()

 print c1.dic
 {'1': '2', '2': '2'}
 print c2.dic
 {'1': '2', '2': '2'}
 
 This is not what I have excepted.
 Although I know the solution to get what I want...
 class base():
 ...   def __init__(self):
 ... self.dic = {'1':'1', '2':'2'}
 ...
 class child1(base):
 ...   def __init__(self):
 ... base.__init__(self)
 ... self.dic.update({'1':'2'})
 ...
 class child2(base):
 ...   pass
 ...
 c1 = child1()
 c2 = child2()

 print c1.dic
 {'1': '2', '2': '2'}
 print c2.dic
 {'1': '1', '2': '2'}
 
 ... I wonder if there is a special reason for the behaviour in the
 first example.
 Shouldn't the first example behave like the second?

No, because you are creating *classvariables* when declaring things like
this:

class Foo(object):
   bar = {}

If these are mutable, changing them will change them for *all* instances.

OTOH, when assigning to an instance, this will create an
*instance*-variable. Which is what

self.some_name = some_value

does.

Diez
--
http://mail.python.org/mailman/listinfo/python-list


Variables vs attributes [was Re: Inheriting dictionary attributes and manipulate them in subclasses]

2009-04-17 Thread Steven D'Aprano
On Fri, 17 Apr 2009 17:48:55 +0200, Diez B. Roggisch wrote:

 No, because you are creating *classvariables* when declaring things like
 this:
...
 OTOH, when assigning to an instance, this will create an
 *instance*-variable. Which is what


If an integer variable is an integer, and a string variable is a string, 
and float variable is a float, and a list variable is a list (there's a 
pattern here), shouldn't a class variable be a class and an instance 
variable be an instance?

I had never noticed the usage of variable to mean attribute until a few 
months ago. What's going on? Why did people decide that confusing 
variables and attributes of variables was a good thing? What's next, 
describing dictionary keys as dictionary variables?

Sorry to pick on Diez, he's not the only one, just the latest example.


-- 
Steven
--
http://mail.python.org/mailman/listinfo/python-list