subclass constructor problem

N

Nikola Skoric

I have a superclass Element and a subclass Family. All Family.__init__() does is run Element.__init__() and self.__parse(). For some reason it seems like self.__parse() isn't run. Here is the code:
http://github.com/dijxtra/simplepyg...cce6b97c0b5e75b8be/simplepyged/simplepyged.py

In this version everything works fine. Notice lines 698, 703 and 708. If I remove those lines, those 3 methods return None or []. Seems like __init__ won't run __parse, but other methods run it without problem.

So, what obvious thing am I missing? :)
 
D

Diez B. Roggisch

Nikola Skoric said:
I have a superclass Element and a subclass Family. All Family.__init__() does is run Element.__init__() and self.__parse(). For some reason it seems like self.__parse() isn't run. Here is the code:
http://github.com/dijxtra/simplepyg...cce6b97c0b5e75b8be/simplepyged/simplepyged.py

In this version everything works fine. Notice lines 698, 703 and 708. If I remove those lines, those 3 methods return None or []. Seems like __init__ won't run __parse, but other methods run it without problem.

So, what obvious thing am I missing? :)

Works without a hitch for my condensed example:

class Element(object):

def __init__(self):
pass


class Family(Element):

def __init__(self):
Element.__init__(self)
self.__parse()


def __parse(self):
self.parse = "Family.__parse"


f = Family()
print f.parse



Btw, you are a bit on the overprotective side. The convention for
marking attributes (methods or objects alike) "private" is by prefixing
them with a *single* underscore.

Diez
 
B

bruno.desthuilliers

Btw, you are a bit on the overprotective side. The convention for
marking attributes (methods or objects alike) "private"

s/private/implementation/

I find that thinking in terms of "interface / implementation" instead
of "public / private" really helps focusing on what's important here.
is by prefixing
them with a *single* underscore.

And FWIW, the usual idiom is to avoid dummy accessor and use plain
attributes until you have a need for a computed one - in which case
you use a descriptor (either the builtin 'property' or custom
descriptor). Python is not Java.
 

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
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top