mijmajmoj
mijmajmoj

Reputation: 59

How can I use attributes from a parent class in a child class

I am writing a chess program but don't quite understand inheritance. How can I use an attribute (empassant) from a parent class in a subclass? My parent class is:

class Pieces():
    def __init__(self, empassant=(-5,-5)):
        super().__init__()
        self.empassant=empassant

My subclass is:

class White (Pieces):

    def __init__(self):
        #stuff
    def pawn(self, pieceposition):
        empassant=#empassant from the pieces class

Upvotes: 0

Views: 69

Answers (1)

Narcisse Doudieu Siewe
Narcisse Doudieu Siewe

Reputation: 1104

you need to call super().__init__() first in the __init__ of the child

class White (Pieces):

    def __init__(self):
        super().__init__()
        #others initialization here

    def pawn(self, pieceposition):
        print(self.empassant)

Upvotes: 1

Related Questions