Attribute definition, WHY?
Steve Holden
sholden at holdenweb.com
Tue Sep 12 13:51:08 EDT 2000
bragib at my-deja.com wrote:
>
> Why would you want to do the following:
>
> class A:
> attr1 = [1,2]
> def __init__(self,name):
> self.name = name
>
In this case, attr1 becomes a shared object, or class attribute,
available to all instances (and, perhaps significantly, all
instances will refer to and possibly update a single copy).
> and not
>
> class A:
> def __init__(self,name):
> self.name = name
> self.attr1 = [1,2]
>
In this case case attr1 is an instance attribute, so each instance
of an A will have its own attr1.
> Are there benefits to the first definition?
>
The first definition might be appropriate if you want inter-instance
communication via a shared attribute. Such behavior has been used,
for example, to track the number of instances by incrementing a
shared counter in the __init__ method.
> Bragi
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
Hopet his helps
regards
Steve
--
Helping people meet their information needs with training and technology.
703 967 0887 sholden at bellatlantic.net http://www.holdenweb.com/
More information about the Python-list
mailing list