python - Why aren't my @property decorators working? -
i'm working on little class, playing @property decorators. reason it's not working right.
class card: def __init__(self, rank, suit): self.rank = rank self.suit = suit @property def rank(self): return self._rank @rank.setter def rank(self, value): valid_ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "j", "q", "k", "a"] if valid_ranks.index(value): self._rank = value @property def suit(self): return self._suit @suit.setter def suit(self,value): self._suit = value def show(self): print "{}{}".format(self.rank, self.suit)
i initialize object so: card("cheese", "ball")
, , presumably should throw error. python happily rolls though , assigns "cheese" rank. what's going on here? other calls rank setter through assignment syntax seem happily ignore setter i've put in place. i'm running python 2.7.5.
you're using old-style classes. noted in the documentation, properties don't work old-style classes. derive classes object
. e.g.:
class card(object): ...
in python 3 don't need this, because python 3 has new-style classes.
Comments
Post a Comment