deleting attributes

Richie Hindle richie at entrian.com
Wed Aug 28 05:36:38 EDT 2002


> you can delete attributes [...] This seems dangerous to me.
> Can anyone give me a legitimate reason or eg for why you would
> want to do this?

Sometimes this is very useful, for instance if your Python objects
represent something for which adding and deleting attributes is a
natural thing to do.  The action of adding or deleting an attribute
for an object can call Python code, via the __setattr__ and
__delattr__ special methods, and hence have side-effects.  This
lets you model problems in an intuitive way - the code that
manipulates the objects modelling your problem is very easy to
read.  For instance, if your objects represent a database, one
intuitive way to delete the 'telephone' column from the 'customer'
table is like this:

>>> del database.customer.telephone

That would do work behind the scenes as well as deleting the
'telephone' attribute from 'customer' object, but you as the user
of the database object don't need to know that.

-- 
Richie



More information about the Python-list mailing list