简体   繁体   中英

Accessing a class/method from another program in Python

I have a program ( blackjack.py ) and it accesses another program's ( cards.py and games.py ) within its code. Most of this is from a book, so I'm having trouble understanding how it works.

Heres the code for cards.py :

class Card(object):
    """ A playing card. """
    RANKS = ["A", "2", "3", "4", "5", "6", "7",
             "8", "9", "10", "J", "Q", "K"]
    SUITS = ["c", "d", "h", "s"]
    def __init__(self, rank, suit, face_up = True):
        self.rank = rank
        self.suit = suit
        self.is_face_up = face_up

    def __str__(self):
        if self.is_face_up:
            rep = self.rank + self.suit
        else:
            rep = "XX"
        return rep

    def flip(self):
        self.is_face_up = not self.is_face_up


class Hand(object):
    """ A Hand of playing cards. """
    def __init__(self):
        self.cards = []

    def __str__(self):
        if self.cards:
            rep = ""
            for card in self.cards:
                rep += str(card) + "\t"
        else:
            rep = "<empty>"
        return rep

    def clear(self):
        self.cards = []

    def add(self, card):
        self.cards.append(card)

    def give(self, card, other_hand):
        self.cards.remove(card)
        other_hand.add(card)


class Deck(Hand):
    """ A deck of playing cards. """
    def populate(self):
        for suit in Card.SUITS:
            for rank in Card.RANKS:
                self.add(Card(rank, suit))

    def shuffle(self):
        import random
        random.shuffle(self.cards)

    def deal(self, hands, per_hand = 1):
        for round in range(per_hand):
            for hand in hands:
                if self.cards:
                    top_card = self.cards[0]
                    self.give(top_card, hand)
                else:
                    print "Can't continue deal. Out of cards!"

if __name__ == "__main__":
    print "This is a module with classes for playing cards."
    raw_input("\n\nPress the enter key to exit.")

I'm writing an error check for the blackjack.py and I need to gather the number of cards that have been used so far. I think I can do that by accessing the number of values in cards[] . The problem is, I am not sure on how to do that.

This is all in theory though. I will include the `blackjack.py' code as well, so you all can see what I am trying to do, and help me determine if my logic is flawed.

blackjack.py code

Any and all input is appreciated.

While I'm not entirely clear on your intended structure here, you have a couple of options.

First, in order to use any functions or classes from cards.py in your blackjack.py module, you can use the import statement to import them. There are two styles of doing this:

# blackjack.py
import cards

Would give you access to everything in the cards module, and you'd call each function/class by prefacing it with cards.<name> . So, if you wanted to initialize an instance of the Deck class,

# blackjack.py
import cards
mydeck = cards.Deck()

The other way is the from <X> import <Y> , which gives you access to the functions and classes without having to add the prefix. Example:

# blackjack.py
from cards import Deck  # or, to import everything, "from cards import *"
mydeck = Deck()

Either of these methods would give you an instance of the cards.Deck class.

Option 0

You can already tell this within your Deck class, since it subclasses from Hand , so every time you give a card it's taking out out of the Deck 's cards attribute. Thus, the number of cards given out would simply be:

class Deck(Hand):
    # ...
    def number_cards_used(self):
         return 52 - len(self.cards)

Alternatively, if you can't edit cards.py , you can simply get the number of cards left from your given Deck by:

# blackjack.py
def get_number_cards_used_from_deck(deck):
    return 52 - len(deck.cards)

In use:

# blackjack.py
import cards
mydeck = cards.Deck()
# ...
# Do other operations with deck
# ...
cards_used = get_number_cards_used_from_deck(mydeck)

Option 1

If you can isolate all of and only those hands being played simultaneously, you can implement a method card_count :

class Hand(object):
   # ...
   # other code
   # ....

   def card_count(self):
       return len(self.cards)

Then, if you have a list of all the hands, for example, you could do something like:

sum(map(lambda h: h.card_count(), list_of_hands))

Option 2

Within your Deck class, since it subclasses Hand , you could simply keep another running list of the cards that have been given out that would often be refreshed. This would look like:

class Deck(Hand):
    # ...
    # other code
    # ...

    def __init__(self):
        self.populate()
        self.used_cards = []

    def give(self, card, other_hand):
        self.used_cards.append(card)
        super(Deck, self).give(card, other_hand)

    def number_cards_used(self):
        return len(self.used_cards)

There are other methods, of course.

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