Inheritance/Late Private Binding

J

Jeremy Moles

class BaseClass:
def __init__(self):
self.__data = None

def getMember(self):
return self.__data

class GoodSubClass(BaseClass):
def __init__(self):
BaseClass.__init__(self)

class BadSubClass(BaseClass):
def __init__(self):
self.__x = None

gsc = GoodSubClass()
print dir(gsc)
gsc.getMember()

bsc = BadSubClass()
print dir(bsc)
bsc.getMember()

------------------------------------------------

Forgive me if this topic has been brought up before, but I was curious
as to why I was getting this behavior and was hoping someone
knowledgeable could explain. :)

I "feel" like even without the explicit call to a simple base ctor(),
mangling should still happen correctly. This doesnt, however, seem to be
the case...

Of note: simply doing a 'pass' in BadSubClass seems to be sufficient as
well; so, it has something to do with defining a ctor() in the child and
not explicitly invoking the parent's ctor.
 
M

Michael Hoffman

Jeremy said:
Forgive me if this topic has been brought up before, but I was curious
as to why I was getting this behavior and was hoping someone
knowledgeable could explain. :)

What behavior?
I "feel" like even without the explicit call to a simple base ctor(),
mangling should still happen correctly. This doesnt, however, seem to be
the case...

How is mangling happening incorrectly?

Functions in base classes that are overridden by subclasses aren't
called implicitly.

Also, I think using the term "ctor()" is confusing since you don't seem
to have anything callable named ctor. As for the constructor, just call
it __init__, it will avoid confusion with __new__.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top