declaration of variables?

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


X"A. Lloyd Flanagan" <alloydflanagan at attbi.com> wrote ...
> Jp Calderone <exarkun at intarweb.us> wrote ...
> >   I'd think it was obvious.  Right now, I can bind any name any time I
like
> > .
> > 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
any
> > more, I'd have to go find where it was declared and remove it.  If you
think
> > this isn't a significant hurdle, I recommend you go read some of the
larger
> > C projects out there.  In many of them, you'll find all kinds of accrued
> > variable cruft - variables people declared, used, and then discarded,
but
> > failed to remove the declarations.  This reduces program readability,
and
> > 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.

regards
--
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Register for PyCon now!            http://www.python.org/pycon/reg.html






More information about the Python-list mailing list