简体   繁体   中英

calling one class's method inside another in python

How could I call the shuffle and takecard methods, belonging to the Deck class and call it in the Dealer class without inheriting the whole class.

#!/usr/bin/python
import random
class Card:
    """A card object with a suit and rank."""
    RANKS = (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
    SUITS = ('Spades', 'Diamonds', 'Hearts', 'Clubs')
    def __init__(self, rank, suit):
       self._rank = rank
       self._suit = suit
    def getRank(self):
       return self._rank
    def getSuit(self):
       return self._suit
    def __str__(self):
       translate = {11:'Jack', 12:'Queen', 13:'King', 14: 'Ace'}
       r = self._rank
       if r in [11, 12, 13, 14]:
          myrank = translate[r]
       else:
          myrank = str(r)
       return myrank + " of " + self._suit
    def __lt__(self, other):
        return( self._rank < other.getRank() )
class Deck:
    def __init__(self):
        self._cards = []
        for suit in Card.SUITS:
            for rank in Card.RANKS:
                c = Card(rank, suit)
                self._cards.append(c)
    def shuffle(self):
        random.shuffle(self._cards)
    def takecard(self):
        if len(self._cards) == 0:
            return None
        else:
            #print self._cards.pop()
            return self._cards.pop()
    def __len__(self):
        return len(self._cards)
    def __str__(self):
        result = ""
        for c in self._cards:
            result = result + str(c) + '\n'
        return result
class Dealer(object):
    def __init__(self, name):
        self.name = name
    def shuffle(self):
        deck=self.deck
        self.deck.shuffle()     
    def deal(self):
        self.deck.takecard()

self.deck.takecard and self.deck.shuffle doesn't return the Deck methods if I declare it this way: Dealer('Bob',Deck)

If you want to pass in an instance of Deck as you create an instance of Dealer, attach the deck to the dealer as an attribute, and then call the deck's functions from within the dealer's functions, you can do something like this:

class Dealer(object):
    def __init__(self, name, deck):
        self.name = name
        self.deck = deck
    def shuffle(self):
        self.deck.shuffle()
    def deal(self):
        return self.deck.takecard()

The object you pass in as the argument 'deck' will need to be an instance of Deck:

a_deck = Deck()

I believe the problem with your code is that you need to return from the Dealer functions, not just from the Deck's functions, like below. You also need to set the deck in your initialiser:

class Dealer(object):
    def __init__(self, name, deck):
        self.name = name
        self.deck = deck

    def shuffle(self):
        deck=self.deck
        return self.deck.shuffle()
    def deal(self):
        return self.deck.takecard()

Finally, you should init this class with:

my_dealer = Dealer("Bob", Deck())

Just using Deck without the parenthesis will result in the class being given to the dealer, not an instance of the class, which is what you want.

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