Why does my class not inherit from the parent class?

56 views Asked by At

I am creating a subclass, but I am having difficulties making it inherit from the parent class:

def ParentClass(object):

    def __init__(self,num):
        self.num = num
        self.get_soup()

    def get_soup(self):
        self.soup = 'soup'
        return self.soup

def SubClass(Advert):

    def __init__(self,num):
        ParentClass.__init__(self,num)

    def test(self):
        print 'it works'
        print self.num

if __name__== "__main__":

    num = 1118868465    
    ad = SubClass(num)
    ad.test()

Should I have a look at metaclasses?

1

There are 1 answers

0
Padraic Cunningham On

You have functions in your code not classes, the parent class is also called ParentClass not Advert:

class  ParentClass(object): # class not def
    def __init__(self,num):
        self.num = num
        self.get_soup()

    def get_soup(self):
        self.soup = 'soup'
        return self.soup

class SubClass(ParentClass): # inherit from ParentClass
    def __init__(self, num):
        super(SubClass, self).__init__(num)
    def test(self):
        print 'it works'
        print self.num

You might want to have a read of this tutorial