While trying to come up with a Hand class for a card game I encountered a strange behavior from an attribute
if I try to set self.number as seen below it wont show the proper output
but if I make the same argument through a function total() it works properly
my question is: why does the attribute self.number not getting the value of len(self.cards)?
class Hand (object):
def __init__(self,number=0,cards=[]):
self.cards=cards
self.number=len(self.cards)
def total(self):
return len(self.cards)
hand=Hand()
hand.cards.append(9)
print hand.cards
print len(hand.cards)
print hand.number
print hand.total()
output:
[9]
1
0 #I want this to be equal to 1
1