Inheritence confusion (was Re: Deeply-nested class layout suggestions wanted)
Robert Brewer
fumanchu at amor.org
Fri Jun 11 16:47:44 EDT 2004
Kirk Strauser wrote:
> As a final concrete example, I whipped up a few little
> skeleton classes like
> so:
>
> Generic
> |-- Subclass
> | |-- __init__.py
> | |-- a.py
> | `-- b.py
> |-- __init__.py
> |-- a.py
> `-- b.py
>
> Generic/a.py
>
> from b import b
>
> class a:
> def __init__(self):
> test = b()
>
> Generic/b.py
>
> class b:
> def __init__(self):
> print 'In Generic.b'
If it were my code, I'd rewrite Generic/a.py as:
from Generic import b
class a(object):
def __init__(self):
test = b.b()
> I'm completely lost. What do I have to do to get the
> subclasses to use modules at their own level instead of where
> the defined __init__ function happens to be?
Some options:
1) Override __init__ in the subclass.
2) Pass the correct module as an argument to init.
3) Perform ugly module-inspection hacks. Don't do this.
It would be a *lot* clearer if your example didn't re-use names in different modules. Try some other letters once in a while. :) It'll make your design clearer. Seriously, rewrite your example without duples and I can spare more brain cells on it.
Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org
More information about the Python-list
mailing list