1

This is my class:

class Player(object):
    def __init__(self, playernum):
    self.playernum = playernum

    def play_turn(self, board):
        """This method is passed an instance of ConnectFour.  
           It should examine the board (using methods on the ConnectFour class...
           assume you have it) and eventually call board.play_turn and return"""
    pass

So far I understand that if I do:

class Human(Player):

It will make Human() a derived class of Player.

What I would like to do is have a constructor playernum inside this class. Then take the overridden play_turn and print a player number(ie. playernum)...I just want to know how this would be implemented... do I repeat

def play_turn(self,board):

inside the Human class or do I simply put

class Human(Player):
    play_turn

and inside the

play_turn(self,board):
    "put"
    print playernum

I'm kind of new to derivations of classes and the logic behind it. Any input will be highly appreciated. Thanks.

2
  • What exactly do you mean by "a constructor playernum"? A constructor is the __init__ function (from inside the class) or the calling to the class itself (from outside the class). So this phrase doesn't make any sense. Commented Mar 14, 2013 at 20:43
  • @abarnert I meant that playernum from the Player class is used within the parameters of the Human class so I can use the playernum Commented Mar 14, 2013 at 20:56

1 Answer 1

4

You're correct that to override a method from a parent class, you 'repeat' the method inside the derived class. Your code should end up looking something like:

class Human(Player):
    def play_turn(self, board):
        print self.playernum

If play_turn is meant to contain shared logic for its derived classes, you want to call the parents' method first:

class Human(Player):
    def play_turn(self, board):
        super(Human, self).play_turn(board)
        print self.playernum
Sign up to request clarification or add additional context in comments.

1 Comment

What if I wanted to add a prompt within this class do i just put 'var = raw_input("which column would you like to drop the chip? ")' and then print that with 'print var'? It seems I'm overthinking this...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.