Python dumps core with this.. how come?
Tim Peters
tim_one at email.msn.com
Sat Jan 22 16:16:23 EST 2000
[Interrupting Michael Hudson's fine advice to inject a nit]
[Roey Katz]
> this brings up another concern. Given
> the following definition:
>
> class A:
>
> def __getattr__( self, name ):
> return -1
> def __setattr__( self, name, val ):
> self.__dict__[ name ] = val
>
> I see that self.__dict__ must be a special variable
> in that setting it somehow avoids a recursive call
> to __setattr__()?
[Michael]
> Yup. Look in Objects/classobject.c around about line 580
> if you want the gory details.
While true, it doesn't apply to Roey's example: he's not setting __dict__.
He's binding a key *of* self.__dict__; self.__dict__ itself is merely
referenced. So Roey should really be wondering why the reference to
self.__dict__ isn't invoking __getattr__ (&, of course, that's also due to
special-case __dict__ magic, but in __getattr__'s implementation).
I don't mean to be overly picky; it's just that the excruciating details are
vital when mucking with __getattr__/__setattr__, and *any* small
misconception about them will eventually bite hard.
> ...
> Bizarrely,
>
> filter(dest.append,src)
>
> is probably more memory efficient (the map form conses up a
> list of return values, but as append always returns None -
> which is false - filter `rejects' every element) - but if
> you write code like this, you deserve to lose.
Yes, pointing him to the most bizarre endcases of Python behavior will
surely aid him in "resetting his brain" <wink>.
Guido gave some excellent semi-exasperated advice (similar in spirit to
parts of Michael's advice that I've snipped) many moons ago, when the whole
newsgroup was temporarily filled with msgs of this character: he invited
people to just *try* writing a Python program without using any __xxx__
hooks. I expect some people were amazed at how much easier their lives got!
If he were to get that exasperated again today, I bet he'd expand his
invitation to include avoiding map, filter, reduce and lambda too.
you're-not-ready-to-use-them-in-python-before-you're-
happy-without-them-ly y'rs - tim
More information about the Python-list
mailing list