Assignment not the same as defining?

Steve Holden sholden at
Wed Oct 3 14:41:13 CEST 2001

"Dale Strickland-Clark" <dale at> wrote in message
news:a70mrt441n8h5bh0sf88rhae2q85v6g4hh at
> "Steve Holden" <sholden at> wrote:
[Dale explains failure to rebind __setattr__()]
> >>
> >From Python 2.0 Reference Manual, section 3.3.2:
> >"""
> >The following methods can be defined to customize the meaning of
> >access (use of, assignment to, or deletion of for class
> >For performance reasons, these methods are cached in the class object at
> >class definition time; therefore, they cannot be changed after the class
> >definition is executed.
> >"""
> >
> >foiled-by-an-optimization-ly y'rs  - steve
> Thanks Steve. What a handy chap you are to have around

A pleasure. Always ready to help anyone who didn't download the manuals with
their Python distribution <0.75 wink> - seriously, all I did was to look up
__setattr__ in the manual!

but can I just
> eject a heartfelt *BOLLOCKS* on this one?
Certainly! But never mind the bollocks, here's the Sex Pistols ...

> Now I need yet another cludge.

Perhaps it's one of those times when your design has wandered just a little
too far up a blind alley, and you now feel constrained by something which
needn't exist if you went back a little and took another, possibly only
slightly different, direction. Not, you understand, that I'm saying I *know*
enough about your current design to be sure on this, simply that I recognise
the signs of frustration from my own experiences. To summarise (or
summarize, as they say over here):

a) You've got these whizzy __setattr__() methods that distinguish between
persistent attributes and "ordinary" ones, applying verification to the
former but not the latter. You are prepared to put up with the overhead
because it makes things easier for client software to set all instance
attributes in the same way.

b) Your __setattr__() method is expensive/special enough that you don't
really want it to be activated until after you've run the __init__ method.

c) Python optimizes the snot out of __setattr__ access (along with the other
magic methods) , so it won't let you do what you want to do.

Over to you: either there's some other obvious solution you've failed to
spot, or you will come back with other questions that will help you to
*find* a better non-obvious solution. Go to it!


More information about the Python-list mailing list