Hello, regarding OOP I have Java background, and I usually think in java when I code OOP in python (which sometimes is actually a bad thing).

#!/usr/bin/python
class Card():
        SUIT = { 'C':'Clubs', 'D':'Diamonds', 'H':'Hearts', 'S':'Spades' }
        VALUES = { '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, \
                        '10':10, 'J':12, 'Q':13, 'K':14, 'A':15 }

        def __init__(self,ctupl):
                """ New card instance.
                'ctupl' = (str,str): denoting (SUIT,VALUES) """
                if type(ctupl) != tuple:
                        raise TypeError("Need (str,str) tuple.")
                self.suit = ctupl[0]
                self.value = ctupl[1]

        def __init__(self,suit,value):
                if type(suit) != str or type(value) != str:
                        raise TypeError("Need str,str as parameters.")
                self.suit = suit
                self.value = value

        def __cmp__(self,other):
                """ Overide __cmp__ method.
                Compare two cards. """
                if Card.VALUES[self.value] < Card.VALUES[other.value]:
                        return -1
                elif Card.VALUES[self.value] > Card.VALUES[other.value]:
                        return 1
                else:
                        # Can go SUIT comparison
                        return 0
        def __str__(self):
                """ Overriding to_string() function """
                return "%s of %s" % (self.value,self.SUIT[self.suit])

class PokerHand():
        SIZE = 5
        RANKS = { 0: "Straight Flush", 1: "Four of a Kind", 2: "Full House", \
                        3: "Flush", 4: "Straight", 5: "Three of a Kind", \
                        6: "Two Pairs", 5: "Pair", 6: "High Card" }

        def __init__(self,player,cards):
                """ New poker hand.
                (str,[cards]) """
                if len(cards) < PokerHand.SIZE:
                        raise ValueError("Invalid hands size: %d/%d" \
                                                % (len(cards),5))
                self.player = player
                self.cards = cards
                self.cards.sort()

        def __str__(self):
                """ Overriding to_string function """
                return "[%s]: %s" % (self.player,\
                                ", ".join([ str(x) for x in self.cards ]))

        def issflush(self):
                res = True
                for i in range(self.SIZE-1):
			print Card.VALUES(self.cards[i].value)
                        if self.cards[i].suit != self.cards[i+1].suit:
                                res = False
                                break
                return res


if __name__ == "__main__":
        print("Poker Hands")
        
        p = PokerHand("Andrei",[Card('C','2'), Card('C','3'), Card('C','4'),\
                        Card('C','5'),Card('C','6')])
        print p
        print ("Is straight flush: %d" % p.issflush())

It seems that the static variable: Card.VALUES is not accessible from PokerHand issflush function. Can you please explain me why. Thanks.

PS: I've flirted with python a year ago, but I am still at a very basic level.

Works fine for me (using the following stripped down version). You should include the actual error when posting; it is not always what you think it means.

class Card():
        SUIT = { 'C':'Clubs', 'D':'Diamonds', 'H':'Hearts', 'S':'Spades' }
        VALUES = { '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, \
                        '10':10, 'J':12, 'Q':13, 'K':14, 'A':15 }
 
        def __init__(self):
            print("Card class instance")


class PokerHand():
        SIZE = 5
        RANKS = { 0: "Straight Flush", 1: "Four of a Kind", 2: "Full House", \
                        3: "Flush", 4: "Straight", 5: "Three of a Kind", \
                        6: "Two Pairs", 5: "Pair", 6: "High Card" }
 
        def __init__(self):
            print("PokerHand class instance")
            print("From PokerHand")
            self.issflush()

        def issflush(self):
            print(Card.VALUES)

PH = PokerHand()
print("\nFrom main")
print(Card.VALUES) 
print("\nissflush")
print PH.issflush()

If I run the code initially pasted with

python poker.py

I get the following error:

Poker Hands
[Andrei]: 2 of Clubs, 3 of Clubs, 4 of Clubs, 5 of Clubs, 6 of Clubs
Traceback (most recent call last):
  File "poker.py", line 72, in <module>
    print ("Is straight flush: %d" % p.issflush())
  File "poker.py", line 59, in issflush
    print Card.VALUES("1")
TypeError: 'dict' object is not callable

To access a dictionary key use print Card.VALUES["1"]
Also in your case there is no key named "1"

To access a dictionary key use print Card.VALUES["1"]
Also in your case there is no key named "1"

Thanks both for your answer, now I realize how dumb my question was. Sometimes I get blind...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.