Proposal: default __init__

Rainer Deyke root at rainerdeyke.com
Tue Nov 14 01:17:02 CET 2000


"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:8upr431314b at news2.newsguy.com...
> "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.

I was under the misconceptions that getattr doesn't search the inheritance
hierarchy for classes.  It seems to be a common misconception. :-(


> > 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:-).

getattr3 seems more useful to me as the default, but changing the semantics
of A.b would probably break a lot of code.  Maybe in Python 3000?

> 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...

metaclasses (ab)use the inheritance syntax.  If I wanted that, I could just
use regular inheritance:

class Base:
  def __init__(self):
    pass
  def __del__(self):
    pass
  ...

class A(Base):
  pass # No __init__ yet.

class B(A):
  def __init__(self):
    A.__init__(self)


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list