Adding attribute to objetcs

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Jun 5 21:58:14 EDT 2006


faulkner a écrit :
(please, don't top-post - corrected)
> 
> Miguel Galves wrote:
> 
>>Hello,
>>
>>I`m starting to learn python, and I hava a very good background in Java
>>and C/C++ programming. I was reading Dive into python chapter about
>>OO and I saw that in python we can do the following:
>>
>>class Person:

Do yourself a favor : use new-style classes

class Person(object):
>>    pass
>>
>>joe = new Person()

joe = Person()

No 'new' keyword needed - Python classes are callable objects acting as 
factory.

>>joe.name = "Joe"
>>joe.age = 13
>>
>>It seems that it is possible to add attributes to any object instance
>>in run time, as in Javascript.

Yes, unless you used __slots__ in your class.

>> It seems to me that it can be a source
>>of errors. 

Almost never happened to me in 7+ years of Python programming.

> One that come in my mind is the follwing:
>>
>>class Person:
>>    name = ""

This creates a class attribute (ie: shared by all instances of the 
class). For an instance attribute, you want this:

class Person(object):
   def __init__(self, name="")
     self.name = name

>>joe = new Person()
>>joe.nome = "Joe"
>>
>>The code above adds an attribute called nome,  but the programmer may think
>>it's name.

Then he haven't read it's code !-)

>>What is the real interest of this feature ?

Doing things you can't even dream of in Java or C++. Now if this scares 
you, what about dynamically adding/replacing a method to/of a whole 
class or on a per-instance basis, or changing the class of an object at 
runtime ?-)

> Is there a way to block this
>>kind of error ?

If you're serious, you use automated unit tests, and you'll notice the 
error pretty quickly. Even if you don't use automated unit-tests, the 
test/code cycle in Python is so fast that you'll still notice the 
problem pretty soon. And also, there are tools like pylint that may help 
you catch a lot of typos.

Now in practice, I can assure you this is nothing to worry about. Since 
Python doesn't get in the way, one usually stay very focused on the 
actual code being written instead of fighting with boiler-plate.

 > when you set an attribute of an object, python secretly calls that
 > objects __setattr__ method.
 > class test:

*please* use new-style classes.

 >     def __setattr__(self, attr_name, attr_value):
 >         print self, attr_name, attr_value

stdout is for normal program outputs. Trace etc should go either to a 
log and/or to stderr.

 >         self.__dict__[attr_name] = attr_value    # do what the original
 > __setattr__ method does.

And ? How will this "block this kind of error" ? Are you suggesting to 
test for names in __setattr__ ? This would be totally stupid IMHO. 
Python *is* dynamic. period. Better to go with the language than to try 
to force it into a brain-dead mockup of Java.




More information about the Python-list mailing list