What's better about Ruby than Python?
Andrew Dalke
adalke at mindspring.com
Thu Aug 21 12:49:12 EDT 2003
Alex Martelli:
> That's an oldstyle class -- use a newstyle one for smoothest
> and most reliable behavior of descriptors
Oops! Yeah, forgot that too. I do consider it a (necessary)
wart that different classes have different behaviours.
> > Is that right?
>
> Yes! So what is it that you say you don't get?
Before this I didn't realize the process of __getattr__
had changed to allow the __get__ to work. I thought
properties were done at assignment time rather than
lookup time, and that the hooks were located in
something done with __getattribute__
After stepping through it, with Raymond's descriptor
next to me, I think I now understand it.
> If obj is such that it can be used as a key into a dict
> (weak or otherwise), sure. Many class instances of some
> interest can't -- and if they can you may not like the
> result. COnsider e.g.
>
> class Justanyclass:
> def __init__(self, x): self.x = x
> def compute(self, y): return self.x + y
>
> pretty dangerous to cache THIS compute method -- because,
> as a good instance method should!, it depends crucially
> on the STATE of the specific instance you call it on.
But if someone were to use a method call cache on it
then I would have expected that person to know if its
use was relevant.
> Anyway, I just wanted to
> show how the descriptor concept lets you use a class,
> rather than a function, when you want to -- indeed any
> function now has a __get__ method, replacing (while
> keeping the semantics of) the old black magic.
Yep. Using a class is to be prefered over my
def-with-nested-scope trick.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list