繁体   English   中英

__str__输出“0x00E4F558处的实例”

[英]__str__ outputs “instance at 0x00E4F558”

这是我第一次在这个网站上寻求帮助。 我在这里寻找答案,但没有什么能真正为我清除。

我正在努力学习Python。 我在“课堂”一章。 以下代码:

class Character:
    def __init__(self, name, initial_health):
        self.name = name
        self.health = initial_health
        self.inventory = []

    def __str__(self):
        s  = "Name: " + self.name
        s += "| Health: " + str(self.health)
        s += "| Inventory: " + str(self.inventory)
        return s

    def grab(self, item):
        self.inventory.append(item)

me = Character("vic", 37)
me.grab("pencil")
me.grab("paper")
print str(me)

...生产:

Name: vic| Health: 37| Inventory: ['pencil', 'paper']

但是下面的代码,在我看来非常相似,打印出内存位置而不是变量本身:

import random
SUITS = ['C', 'S', 'H', 'D']
RANKS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']
VALUES = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10, 'K':10}

# define card class
class Card:
    def __init__(self, suit, rank):
        if (suit in SUITS) and (rank in RANKS):
            self.suit = suit
            self.rank = rank
            self.value = self.get_value()
        else:
            print "Invalid card: " + str(suit) + str(rank)
    def __str__(self):
        return str(self.suit) + str(self.rank)
    def get_cardDetails(self):
        return self.suit + self.rank + ": " + str(self.value)
    def get_suit(self):
        return self.suit
    def get_rank(self):
        return self.rank
    def get_value(self):
        value = VALUES[self.rank]
        return value

class Hand:
    def __init__(self, card1, card2):
        self.cardCount = 0
        self.cards = []
        self.cards.append(card1)
        self.cardCount += 1
        self.cards.append(card2)
        self.cardCount += 1
        self.value = self.get_value()
        print "Dealed hand: " + str(self.cards[0]) + str(self.cards[1])
        print "Dealed hand: " + str(self.cards)
    def __iter__(self):
        return iter(self.cards)
    def __str__(self):
        return str(self.cards)
    def get_details(self):
        print "Hand: "
        for each in self.cards:
            print " -" + str(each) + ": " + str(each.value)
        s = "Cards: " + str(self.cardCount) + \
               " | Hand: " + str(self.cards) + \
               " | Total Value: " + str(self.value) + " |"
        return s
    def add_card(self, card):
        self.cards.append(card)
        self.cardCount += 1
        self.value = self.get_value()
        print "Added: " + str(card)
    def get_value(self):
        value = 0
        for each in self.cards:
            value = value + each.value
        return value


c1 = Card("C", "2")
c2 = Card("C", "3")
h = Hand(c1, c2)
print str(h)

输出:

Dealed hand: C2C3
Dealed hand: [<__main__.Card instance at 0x00E4F558>, <__main__.Card instance at 0x00E543C8>]
[<__main__.Card instance at 0x00E4F558>, <__main__.Card instance at 0x00E543C8>]

我敢肯定我必须在这里遗漏一些东西 - 毕竟我只是在学习这个。 任何帮助将不胜感激。 提前致谢。 抱歉这篇长篇文章。

列表的__str__方法在列表的元素上调用__repr__ ,而不是__str__ 您可以遍历列表,将元素转换为自己的str,或者编写一个Hand类,从列表中__str__子类,并定义__str__以在其元素上调用__str__

这取决于你要打印的内容。 在Python中,如果要打印对象,则首先通过评估a.__str__() a其转换为字符串。 如果您的类定义了__str__方法,Python会使用它。 否则它使用来自object__str__方法(在新样式类和Python 3.x中)。

无论如何打印对象的方式取决于__str__方法的实现方式。 如果你正在评估str(els) ,其中els是一个列表,那么Python实际上调用List.__str__(lis)并且这个函数以这样的方式实现,即打印包含在其中的对象的repr (并且函数repr默认返回如您所见的输出。)

如果要使用str方法的对象,可以编写:

[str(el) for el in els]

其中els是您的对象列表。

列表打印其元素的repr() ,而不是str() 加:

__repr__ = __str__

到你的Card类定义。 这将使您的Card类的repr()与它的str()相同,并且列表将按照您的预期打印您的卡片。

你可以__str__ Hand__str__来获取str()而不是列表项的repr()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM