declaration of variables?

Steve Holden sholden at
Wed Feb 26 00:15:25 CET 2003

X"A. Lloyd Flanagan" <alloydflanagan at> wrote ...
> Jp Calderone <exarkun at> wrote ...
> >   I'd think it was obvious.  Right now, I can bind any name any time I
> > .
> > With declarations, any time I wanted a new variable, I'd have to type an
> > extra keyword somewhere.  Any time I decided I didn't need a variable
> > more, I'd have to go find where it was declared and remove it.  If you
> > this isn't a significant hurdle, I recommend you go read some of the
> > C projects out there.  In many of them, you'll find all kinds of accrued
> > variable cruft - variables people declared, used, and then discarded,
> > failed to remove the declarations.  This reduces program readability,
> > would presumably slow down program execution in Python as well.
> >
> I agree, but I think we're missing another feature of not requiring
> declaration.  One class can add attributes to another class.  This is
> useful in all sorts of situations, because you can add behavior to
> class instances without caring what class the instance belongs to.
> Want to add a unique ID to a collection of various objects?
> for x in some_collection:
>       x.unique_id = getUniqueId()
Useful it may be, but of course it's a violation of encapsulation (objects
should really be "black boxes" with defined interfaces) as is any direct
manipulation of instance variables.

While we are rather blasé about such issues in the Python world, your
opinion of this technique's usefulness might go down a little if you ever
spend half a day trying to find out what's wrong because you are overwriting
an existing attribute.

> How you'd preserve the capabilities of augmentation and metaclass
> programming while requiring variable declaration, I don't know.
> Incidentally, that's why I dislike __slots__.  It may have some uses,
> but it means there are a lot of useful things you can't do to objects
> of that class.

In return for which you get some advantages.

Steve Holden                        
Python Web Programming       
Register for PyCon now!  

More information about the Python-list mailing list