Félix Cantournet
Félix Cantournet

Reputation: 1991

Python Class inheritance Constructor fail: What am I doing wrong?


I have a small Python OOP program in which 2 class, Flan and Outil inherit from a superclass Part.
My problem is when I call Flan everything works perfectly, however when I call Outil the program fails silently.
The Outil instance is created, but it lacks all the attributes it doesn't share with Part.
The Outil instance isn't added to Outil.list_instance_outils, nor to Part.list_instances.

class Outil(Part):

    list_instance_outils = []    

    def __init___(self, name, part_type, nodes, elems):
        Part.__init__(self, name, part_type, nodes, elems)
        self.vect_norm = vectnorm(self.nodes[self.elems[0,1:]-1, 1:])
        self.elset = Elset(self)
        self.nset = Nset(self, refpoint=True, generate=False)
        self.SPOS = Ab_surface(self, self.elset)
        self.SNEG = Ab_surface(self, self.elset, type_surf='SNEG')
        Outil.list_instance_outils.append(self)
        Part.list_instances.append(self)

class Flan(Part):

    list_instances_flans = []    

    def __init__(self, name, part_type, nodes, elems):
        Part.__init__(self, name, part_type, nodes, elems)       
        self.vect_norm = vectnorm(self.nodes[self.elems[0,1:4]-1, 1:])
        self.elset = Elset(self)
        self.nset = Nset(self)
        self.SPOS = Ab_surface(self, self.elset)
        self.SNEG = Ab_surface(self, self.elset, type_surf='SNEG')
        Flan.list_instances_flans.append(self)        
        Part.list_instances.append(self)

Both this Classes inherit from Part :

class Part():

    list_instances = []

    def __init__(self, name, part_type, nodes, elems):
        self.name = name
        self.name_instance = self.name + '-1'
        self.part_type = part_type
        self.elems = elems
        self.nodes = nodes
        offset = np.min(self.elems[:, 1:])-1
        self.nodes[:, 0] -= offset
        self.elems[:, 1:] -= offset

I cannot stress enough that I have no error message whatsoever. What am I doing wrong here ?

Upvotes: 1

Views: 165

Answers (2)

Marcin
Marcin

Reputation: 49826

To solve this sort of problem, the best thing to do is to run the code through the debugger.

Get your classes into the python interpreter (import, paste, whatever you like), then call pdb: import pdb; pdb.run('Outil()'). You can now step through the code to see what is happening.

Upvotes: 1

agf
agf

Reputation: 176780

You wrote __init__ with three trailing underscores instead of two in Outil.

Because of this, it doesn't get called -- Part.__init__ gets called instead. That's why the class is created but it lacks the attributes beyond what are in Part.

Upvotes: 7

Related Questions