How to 'declare' class attributes?
Rainer Deyke
root at rainerdeyke.com
Sat May 26 16:56:55 EDT 2001
"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:9eoq8d0i27 at enews2.newsguy.com...
> """
> 1.- to 'declare' (I mean, bind) all of them in the class
> definition:
>
> class foo:
> bar= None
> baz= 1
> fubar= None
> """
> """
> 2.- to bind them in __init__:
>
> class foo:
> def __init__(self):
> self.bar= ...
> self.baz= ...
> self.fubar= ...
> """
> Solutions 1 and 2 address completely different problems, so
> how can they be better/worse than each other...?
No they don't. So long as the attributes are not mutable objects, they can
both solve the same problem. Consider:
class Counter0:
count = 0
def __call__(self):
self.count = self.count + 1
return self.count
class Counter1:
def __init__(self):
self.count = 0
def __call__(self):
self.count = self.count + 1
return self.count
An instance of class 'Counter0' is identical in external behavior to an
instance of 'Counter1'.
--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games - http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor
More information about the Python-list
mailing list