How to access a child class method from a parent class?

Alec Alameddine

There are a lot of answers saying this is unadvisable, but I found a scenario where I think it's useful. Please correct me if I'm wrong and there's a better way.

I'm building a chess game, where individual pieces inherit from the superclass Chesspiece:

class ChessPiece:
    def __init__(self, pos, color, num, piece):
        ...

Each piece has a method that defines the moves it can take:

class Knight(ChessPiece):
    def __init__(self, pos, color=None, num=''):
        ChessPiece.__init__(self, pos, color, num, self.__class__.__name__)


    def possible_moves(self):
        pos_moves = []

        # Up, Right (1 space, 2 spaces)
        try:
            if 1 <= self.x + 2 <= len(Config.board) and 1 <= self.y - 1 <= len(Config.board):
                if Config.board[self.x + 2][self.y - 1] == '___':
                    pos_moves.append(f'{Config.tile_convert(self.x + 2)}{Config.tile_convert(self.y - 1, True)}')

        except Exception: pass

        #Up, Left
        ...

        return pos_moves

I'd like to implement a move() function. The code for the move() function will be the same for each piece, except for the fact that it has to compare the suggested move with the possible moves, which vary for each piece. I could make a move() function for each piece, but that would just be repeating code 6 times.

So, I'd like to define move() in Chesspiece and reference each piece's possible_moves() function.

Netwave

It is simpler to implement an empty possible_moves in the parent:

class ChessPiece:
    ...
    def possible_moves(self):
        raise NotImplementedError

    def move(self, pos):
            if pos in self.possible_moves():
            ...

Or even return an empty set of movements in the parent class:

def possible_moves(self):
    return set()

But I think the first is better, so it forces all subclasses to implement it in order to been useful.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to access parent class variable in child class inside a child method?

From Javascript

How to call a parent method from child class in javascript?

From Java

How to call a Parent Class's method from Child Class in Python?

From Java

Java : Using parent class method to access child class variable

From Java

Access a child method from another child class

From Dev

How do I call a child's method from the parent class?

From Dev

Access child class variable with parent method

From Dev

How Call child method from parent class

From Dev

Access to property defined in child class from static method in the parent class - javascript

From Dev

Access child class fields in a method called by the parent class constructor

From Dev

How to access the state of a child component from a parent class

From Dev

how to access method of parent class with instance of child class in c#

From Dev

Access the child class properties from a parent

From Dev

Python How to override a class member in the child and access it from parent?

From Dev

how to simply access property of parent class from child class by making object of child class

From Dev

How to access child class variables from parent class

From Dev

How to Implement Parent Class Method From Child Class Methods

From Dev

Access parent property name from child class

From Dev

How to access parent class instance method from child class static method in python?

From Dev

Executing a method from a parent class in a child class

From Dev

How to access parent class fields from child class in Django Python

From Dev

How to access parent class instance attribute from child class instance?

From Dev

Access child's __class__ from a parent's method in Python

From Dev

How to access from child checkbox to child class in different parent divs?

From Dev

How to access base class method *args from child class object?

From Dev

How to access in flutter method in derived class from parent class?

From Dev

How to access the init variable obtained from parent class into child method in python?

From Dev

Python How to call child class method from Parent metaclass

From Dev

How can a child class inherit a class method from its parent that gets a class variable from the child in python?

Related Related

  1. 1

    How to access parent class variable in child class inside a child method?

  2. 2

    How to call a parent method from child class in javascript?

  3. 3

    How to call a Parent Class's method from Child Class in Python?

  4. 4

    Java : Using parent class method to access child class variable

  5. 5

    Access a child method from another child class

  6. 6

    How do I call a child's method from the parent class?

  7. 7

    Access child class variable with parent method

  8. 8

    How Call child method from parent class

  9. 9

    Access to property defined in child class from static method in the parent class - javascript

  10. 10

    Access child class fields in a method called by the parent class constructor

  11. 11

    How to access the state of a child component from a parent class

  12. 12

    how to access method of parent class with instance of child class in c#

  13. 13

    Access the child class properties from a parent

  14. 14

    Python How to override a class member in the child and access it from parent?

  15. 15

    how to simply access property of parent class from child class by making object of child class

  16. 16

    How to access child class variables from parent class

  17. 17

    How to Implement Parent Class Method From Child Class Methods

  18. 18

    Access parent property name from child class

  19. 19

    How to access parent class instance method from child class static method in python?

  20. 20

    Executing a method from a parent class in a child class

  21. 21

    How to access parent class fields from child class in Django Python

  22. 22

    How to access parent class instance attribute from child class instance?

  23. 23

    Access child's __class__ from a parent's method in Python

  24. 24

    How to access from child checkbox to child class in different parent divs?

  25. 25

    How to access base class method *args from child class object?

  26. 26

    How to access in flutter method in derived class from parent class?

  27. 27

    How to access the init variable obtained from parent class into child method in python?

  28. 28

    Python How to call child class method from Parent metaclass

  29. 29

    How can a child class inherit a class method from its parent that gets a class variable from the child in python?

HotTag

Archive