Class Variable Question

Alex Martelli aleaxit at yahoo.com
Mon Apr 9 15:20:12 EDT 2001


"Bob Kline" <bkline at rksystems.com> wrote in message
news:mailman.986838312.6381.python-list at python.org...
    [snip]
> > > It seems to me that the user could just override a class with
> > > unrelated data.
> >
> > Sure, just like he could say x=y-z when actually meaning x=y+z, and
> > a zillion other horrible programming errors.
> >
> > Hopefully the user will then run some _tests_ (what other ways would
> > you suggest for the "plus oops I meant minus" kind or errors to be
> > caught...?), fix the mistakes he then finds as a result of his
> > testing, and have code with fewer errors -- including code free from
> > accidental, erroneous rebindings of all kinds.
>
> Ah, you have to love these "You-mean-you-actually-put-bugs-in-your-
> software-that-you-have-difficulty-finding?" responses, especially when

If they pertain to bugs that are rarely inserted, and pretty trivial
to find when inserted, I guess you do.  People used to certain
languages, which gave them security blankets x and y, seem to
feel that having x and y is oh so crucial -- no matter that they're
really miniscule blankets, covering just about nothing that IS
worth covering... they're all they used to have, so they feel truly
terrified losing them.

> the alternating refrain from Python fans is "Why on earth would you want
> to use a language which doesn't eliminate memory management bugs for
> you?"

>From _Python_ fans?  Never heard that -- it IS a regular (and quite
justifiable) refrain from fans of Java and Eiffel, of course, languages
which DO give you that particular (and more important) blanket _as
well as_ the basically-irrelevant ones (other important ones: reliable
exceptions on index-violation, uninitialized-variables, etc, rather than
random undetected scribbling-over of other unrelated memory areas;
but these crucial safeties, which Python of course also provides, are
obviously *runtime-checks*, so the Java and Eiffel "compile-time is
everything and a half" types don't make all that much of them, even
though their languages do have them:-).

To the typical Python fan, the answer to your question is pretty
obvious -- "for maximum, pedal-to-the-metal speed in that 10%
or so of my code which would otherwise be a bottleneck" (that
small slice of typical Python programs which may benefit by being
recoded as extension-modules in C or C++).  [Actually, with Boost
and other good C++ usage, you don't really need to trade much if
any safety in exchange for the blinding-speed, but that's another
issue^H^H^H^H flamewar...].


> And now for something completely different.  Here's a (possibly) more
> helpful response.  You might want to try something along these lines:

The original poster was very explicitly talking about a *class* object,
not an *instance* object, which is why I didn't provide this pretty
standard part -- it doesn't work on class-objects (nor modules, &c).

> but the example demonstrates that the correct answer to your question
> ("Is there a way to prevent this so users cannot add variables?") is
> actually "Yes."

Only if instances are what you're after (and 'attributes' thereof are
what you mean with 'variables'), not if you're interested in what
everybody else calls variables, and in class-objects &c.


Alex






More information about the Python-list mailing list