Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why isn't my subclass inheriting a method from the superclass?

In a blackjack game I have the following piece of code:

class Hand(object):
    def __init__(self):
        self.hand = []
        
        
    def hand_score(self):
        ...
    
    
class Dealer(object):
    def __init__(self):
        Hand.__init__(self)

Where hand_score() should calculate the score in self.hand and return it’s value. However when I assign dealer = Dealer(), deal the cards and call dealer.hand_score() it gives me the error AttributeError: 'Dealer' object has no attribute 'hand_score'.
The self.hand value is inherited and works as expected when I call dealer.hand.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You aren’t actually inheriting from Hand; you are just inappropriately using Hand.__init__ to initialize an unrelated instance.

Hand has to be listed as a base class in the class definition.

class Dealer(Hand):
    def __init__(self):
        Hand.__init__(self)  # preferably, super().__init__()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading