简体   繁体   中英

Why does my class not inherit from the parent class?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM