New-style classes and special methods
Jack Diederich
jack at performancedrivers.com
Mon Feb 24 02:45:07 EST 2003
On Mon, Feb 24, 2003 at 06:38:57PM +1100, Andrew Bennetts wrote:
> On Mon, Feb 24, 2003 at 07:13:44AM +0000, Carl Banks wrote:
> > Jp Calderone wrote:
> > > [-- text/plain, encoding quoted-printable, 37 lines --]
> > >
> > > Consider this class:
> > >
> > > class Foo:
> > > def __getattr__(self, name):
> > > return lambda arg: arg
> > >
> > > It can be used thusly:
> > >
> > > f = Foo()
> > > print f[10]
> >
> > Sure?
>
> Yep, I'd say he's sure.
>
Confused me at first, too
class Foo:
def __getattr__(self, name):
print name
return lambda x:x
f = Foo()
f[10]
prints
'__getitem__'
So writing it without the obfu
class Foo:
def __getitem__(self, name):
return name
An aside, don't try 'print self' above, it tries to get __repr__
for the class ...
IMO the newer behavior makes more sense; python wasn't meant
to provide you impossible questions to ask at interviews
-jackdied
More information about the Python-list
mailing list