Proposal: default __init__

Alex Martelli aleaxit at yahoo.com
Mon Nov 13 17:18:11 EST 2000


"Rainer Deyke" <root at rainerdeyke.com> wrote in message
news:RaXP5.194843$g6.88413163 at news2.rdc2.tx.home.com...
    [snip]
> If redefining attribute access for classes is impractical, then an
external
> function similar to getattr might be the best solution.
>
> def getattr2(klass, name): # Utility function
>   try:
>     return getattr(klass, name)
>   except AttributeError:
>     pass
>   for base in klass.__bases__:
>     try:
>       return getattr2(base, name):
>     except AttributeError:
>       pass
>   raise AttributeError

Once again: I do not understand the point of this function.

getattr2 appears to have _exactly_ getattr's semantics... e.g.:

>>> class A:
 def foo(self): print 'foo'


>>> class B(A):
 pass

>>> getattr(B,'foo')
<unbound method A.foo>
>>>


...what am I missing...?


> def getattr3(klass, name): # The real thing.
>   try:
>     return getattr2(klass, name)
>   except AttributeError:
>     try:
>       return default_methods[name]
>     except KeyError:
>       raise AttributeError

Ok, this one I do see the potential advantage of (getattr
takes a 3rd, default attribute, but we don't want to look
anything up in the default_methods dictionary unless
getattr fails).  A variant of getattr with this slightly
different semantics would have its uses... as long as
the currently-standard one also stays around:-).

I _suspect_ this is the kind of things you can do today w.
ExtensionClass, or other uses of metaclasses, but my
understanding thereof isn't deep enough, and I know
there are also some current limitations due to hardwired
PyInstance_Check calls in the Python sources...


Alex






More information about the Python-list mailing list