[英]How to properly implement/overload "__repr__ "?
New to python and this might be a silly question, but how does one properly implement the repr method? python 新手,这可能是一个愚蠢的问题,但是如何正确实现repr方法?
I wrote a quick little program to simulate a game of cards but I don't know what to write for the repr method.我写了一个快速的小程序来模拟纸牌游戏,但我不知道为repr方法写什么。 The repr method for the Card class was pretty straight forward, but I don't know what to do for the DeckOfCards class Here's my code: Card 类的repr方法非常简单,但我不知道如何为DeckOfCards 类做以下是我的代码:
import random
class Card:
'''Create a single card, by id number'''
# Class variables, created once for the class
suits = [ '\u2660', '\u2661', '\u2662', '\u2663' ]
ranks = [ 'A','2','3','4','5','6','7','8','9','10','J','Q','K' ]
def __init__(self, n=0):
# instance variables for _num, _rank, _suit, _value
if 0 <= n < 52:
self._num = n
self._rank = Card.ranks[n%13] # note referencing class vars
self._suit = Card.suits[n//13]
self._value = n%13 + 1
if self._rank == 'A':
self._value = 14
else: # invalid card indicators
self._rank = 'x'
self._suit = 'x'
self._value = -1
def __repr__(self):
return self._rank + self._suit
def __lt__(self,other):
return self._value < other._value
def __le__(self,other):
return self._value <= other._value
def __eq__(self,other):
return self._value == other._value
class DeckOfCards:
'''A Deck is a collection of cards'''
def __init__(self):
self._deck = [ Card(i) for i in range(52) ]
def __repr__(self):
return 'Deck : ', self._deck
def shuffle(self):
return random.shuffle(self._deck)
def deal_a_card(self, i=-1):
#that way player can choose where to draw from
return self._deck.pop(i)
def cards_left(self,count):
return len(self._deck)
new_deck = DeckOfCards()
Also, feel free to comment on anything you'd like, whether it be a design flaw or redundancy in code, literally anything.此外,您可以随意评论您想要的任何内容,无论是设计缺陷还是代码冗余,几乎任何事情。 Thanks in advance!提前致谢!
You should return a string type, for example in Deck:你应该返回一个字符串类型,例如在 Deck 中:
def __repr__(self):
...
return 'Deck : '+str(self._deck)
__repr__
ideally could return the representation of the object that you would use to create this instance. __repr__
理想情况下可以返回用于创建此实例的对象的表示。
For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object.对于许多类型,此函数会尝试返回一个字符串,该字符串在传递给 eval() 时将产生具有相同值的对象,否则表示是用尖括号括起来的字符串,其中包含对象类型的名称附加信息通常包括对象的名称和地址。
First, It should be noted that you don't have to implement the __repr__
method.首先,应该指出的是,你不必执行__repr__
方法。 Python provides a somewhat reasonable default (it'll at least tell you the type). Python 提供了一个比较合理的默认值(它至少会告诉你类型)。
If you want to implement __repr__
, the "rule of thumb" is that where it makes sense, you should provide enough information about the object that a user could reconstruct it.如果您想实现__repr__
,“经验法则”是在有意义的地方,您应该提供有关对象的足够信息,以便用户可以重建它。 In your case, there doesn't seem to be any real difference from one deck to another, so在你的情况下,从一个甲板到另一个似乎没有任何真正的区别,所以
def __repr__(self):
return 'Deck()'
might be a reasonable return value.可能是一个合理的返回值。 This doesn't get the state right (after shuffling), but you don't provide an interface for constructing a deck in a particular state.这不会使状态正确(在洗牌后),但是您没有提供用于在特定状态下构建牌组的接口。 If you did, it might look like:如果你这样做了,它可能看起来像:
def __repr__(self):
return 'Deck(%s)' % self._deck
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.