Class Variable Access and Assignment
Graham
graham.abbott at gmail.com
Thu Nov 3 21:02:11 EST 2005
Many thanks your explaination cleared up many of the questions I had.
I know think i can understand the purpose, regardless of my opinion, i
do however think that one should be able to assign the value in the
same way it is accessed.
Given your previous example:
> class Counter(object):
> "A mutable counter."
> # implementation elided
> class A(object):
> instance_count = Counter()
> def __init__(self):
> self.instance_count.increment()
if you changed
> class A(object):
> instance_count = Counter()
> def __init__(self):
> self.instance_count.increment()
to
> class A(object):
> instance_count = 0
> def __init__(self):
> self.instance_count = self.instance_count + 1
It would not work as planned. I understand all the reasons why this
occurs, but i dont understand why its implemented this way. Because it
acts in a different way than you expect. It seems to me that
self.instance_count should not create a new entry in the __dict__ if a
class variable of that name is already present anywhere in that objects
hierarchy.
Does that make sense?
Again thank you for explaination.
graham
More information about the Python-list
mailing list