[Python-Dev] __set_foo__ et al. (PEP-able)

Paul Prescod paul@prescod.net
Thu, 20 Jul 2000 17:03:34 -0500


"M.-A. Lemburg" wrote:
> 
> Paul Prescod wrote:
> >
> > "M.-A. Lemburg" wrote:
> > >
> > > > def __setattr__( name, val ):
> > > >     if attribute_to_set=="__class__":
> > > >         assert PyClass_Check( val ):
> > > >     elif attribute_to_set=="__dict__":
> > > >         assert PyDict_Check( val ):
> > >
> > That's not the case. In this case, Guido is doing type checking on sets.
> > You can only do that in Python with an __setattr__ hack.
> 
> Huh ? "if attribute_to_set == ..." looks like a switch on
> attribute name to me.

But he's doing the switch not for performance reasons but to check the
data value before allowing the assignment! In Python you might do it for
the same basic reason.

> __get_foo__ causes a Python function call and this is about
> the most costly activity there is in Python's internals.

You wouldn't use __get_foo__ unless you *needed to do some computation*.
Therefore you shouldn't compare it to anything OTHER than a Python
function call. The appropriate comparision is to __getattr__ with a big
switch statement or code.

> Well, yes. It works. But tell me: how are you going to debug
> this ? Assignments will suddenly raise exceptions and the
> traceback won't be of any use for programmers to find the
> true error location.

Assignments can raise exceptions today. Try assigning to tuple slot.

The traceback will go through the code that did the assignment. It won't
be at the bottom of the traceback but it will appear.

The traceback will be MORE useful than the altenatives of either a proxy
or __getattr__.

> Too much magic and too many possibilities for overloading
> dots ;-)

Python already gives you the ability to do this magic and we do it all
over the place. The question is whether to make it a little more
reliable, performant and consistent or not.

> (I've never understood why Javascript causes a new URL to be shown
> when your assign to top.window.location.)

Well a Python web-browser could implement the same poor design decision
*today*. I am going to make it easier but I'm not going to force people
to use it!

-- 
 Paul Prescod - Not encumbered by corporate consensus
"Hardly anything more unwelcome can befall a scientific writer than 
having the foundations of his edifice shaken after the work is 
finished.  I have been placed in this position by a letter from 
Mr. Bertrand Russell..." 
 - Frege, Appendix of Basic Laws of Arithmetic (of Russell's Paradox)