Methods of setting class attributes
Hans Nowak
hnowak at cuci.nl
Wed Jul 25 08:40:32 EDT 2001
>===== Original Message From Piotr Legiecki <piotrlg at sci.pam.szczecin.pl>
=====
>hnowak wrote:
>> Assuming you just want the default behavior to set attributes in a class
and
>> get going, this is a common idiom:
>>
>> class Foo:
>> def __init__(self, x):
>> self.x = x
>> self.y = "blah"
>>
>
>Yes. That is assigning in constructor. But what to do if I want to make
>an assignment after the object is created? Thats normal during object
>life. Simply object.attrib=something, etc? I know it works, but I'm not
>sure if it is in python style.
Yes, it's the normal way to do it:
foo = Foo()
foo.x = 37
# etc...
>> Python is much more dynamic than C++ and attributes of a class or instance
can
>> be set or deleted at will.
>
>Right! I'v noticed that and it is not easy to adopt. What about class
>interface? There is simply none in python, because I can change it at
>any time. Strange.
Other people missed this feature too. I think proposals for an interface
mechanism were discussed in the Type-SIG some time ago. Also, there's a PEP
you might be interested in:
http://python.sourceforge.net/peps/pep-0245.html
Regards,
--Hans Nowak
More information about the Python-list
mailing list