Python 3.x Python:打印函数以调用随机生成的类对象
初级python开发人员四处玩并尝试制作纸牌游戏。在我自己的系统上随机选择卡片Python 3.x Python:打印函数以调用随机生成的类对象,python-3.x,Python 3.x,初级python开发人员四处玩并尝试制作纸牌游戏。在我自己的系统上随机选择卡片 import random # list to draw random card from from: card_names = ["ace_h", "two_h", "three_h"] class Cards(object): in_play = False def __init__(self, face, color, value): self.face = face
import random
# list to draw random card from from:
card_names = ["ace_h", "two_h", "three_h"]
class Cards(object):
in_play = False
def __init__(self, face, color, value):
self.face = face
self.color = color
self.value = value
ace_h = Cards("Ace", "Hearts", 14)
two_h = Cards("Two", "Hearts", 2)
three_h = Cards("Three", "Hearts", 3)
random_1 = random.choice(card_names)
print (random_1 +".face")
如果通过随机化选择了ace\u h
,我希望最后的打印将显示打印ace\u.face
(即ace)的结果。相反,打印只是将ace_h.face
显示为字符串,而不是与之关联的实际值
我怎样才能改变这个
请注意,只需使用
print(ace_h.face)
即可获得所需的结果。您可以将所有对象放入如下列表:
objectList = [ace_h, two_h, three_h]
random_1 = random.choice(objectList)
然后打印所需的属性:
print(random_1.face)
您可以将所有对象放入如下列表:
objectList = [ace_h, two_h, three_h]
random_1 = random.choice(objectList)
然后打印所需的属性:
print(random_1.face)
有几种方法可以让你立即想到
import random
class Cards(object):
in_play = False
def __init__(self, face, color, value):
self.face = face
self.color = color
self.value = value
ace_h = Cards("Ace", "Hearts", 14)
two_h = Cards("Two", "Hearts", 2)
three_h = Cards("Three", "Hearts", 3)
# list to draw random card from from:
cards = [ace_h, two_h, three_h]
random_1 = random.choice(cards)
print (random_1.face)
如您所见,我只是对代码重新排序了一点,以便card\u names
可以包含对实际card
对象的引用,而不是它们的名称。这可能是最简单的方法import random
# list to draw random card from from:
card_names = ["ace_h", "two_h", "three_h"]
class Cards(object):
in_play = False
def __init__(self, face, color, value):
self.face = face
self.color = color
self.value = value
ace_h = Cards("Ace", "Hearts", 14)
two_h = Cards("Two", "Hearts", 2)
three_h = Cards("Three", "Hearts", 3)
random_1 = random.choice(card_names)
print(globals()[random_1].face)
这与您的原始代码更相似globals()
返回一个dict
,其中包含在模块级别定义的所有名称,包括函数、类和变量,所有这些名称都按名称键入globals()[random_1]
因此将是一个Cards
对象(顺便说一下,我认为您应该将其重命名为Card
)
在函数内部,您可以使用locals()
为该函数中的本地命名空间获取类似的dict()
。例如,在Cards.\uuuu init\uuuu
中,您可以执行类似locals()['face']
的操作来访问face
参数有几种方法可以让你马上想到
import random
class Cards(object):
in_play = False
def __init__(self, face, color, value):
self.face = face
self.color = color
self.value = value
ace_h = Cards("Ace", "Hearts", 14)
two_h = Cards("Two", "Hearts", 2)
three_h = Cards("Three", "Hearts", 3)
# list to draw random card from from:
cards = [ace_h, two_h, three_h]
random_1 = random.choice(cards)
print (random_1.face)
如您所见,我只是对代码重新排序了一点,以便card\u names
可以包含对实际card
对象的引用,而不是它们的名称。这可能是最简单的方法import random
# list to draw random card from from:
card_names = ["ace_h", "two_h", "three_h"]
class Cards(object):
in_play = False
def __init__(self, face, color, value):
self.face = face
self.color = color
self.value = value
ace_h = Cards("Ace", "Hearts", 14)
two_h = Cards("Two", "Hearts", 2)
three_h = Cards("Three", "Hearts", 3)
random_1 = random.choice(card_names)
print(globals()[random_1].face)
这与您的原始代码更相似globals()
返回一个dict
,其中包含在模块级别定义的所有名称,包括函数、类和变量,所有这些名称都按名称键入globals()[random_1]
因此将是一个Cards
对象(顺便说一下,我认为您应该将其重命名为Card
)
在函数内部,您可以使用locals()
为该函数中的本地命名空间获取类似的dict()
。例如,在Cards.\uuuu init\uuuu
中,您可以执行类似locals()['face']
的操作来访问face
参数对于如何在Python中引用内容,您有一个基本的误解 在Python中,有变量名:
ace_h = 1
two_h = 2
three_h = 3
您可以按名称调用这些值:
print(ace_h) # => 1
您可以将这些值放入另一个集合:
cards = [ace_h, two_h, three_h]
print(cards) # [1, 2, 3]
可以打印特定元素:
print(cards[0])
可以打印随机元素:
print(random.choice(cards))
但这些都是指值1、2、3
通过检查id
,您实际上可以看到:
print(id(ace_h))
print(id(cards[0]))
无论您是使用ace\u h
还是cards[0]
方法来命名值,都是相同的
这对Python中的所有东西都是一样的——它们都是对象。你可以给他们任何类型的名字,不管是变量名,把他们放在列表中,还是放在字典里——只要id(thing)
是相同的,你谈论的是完全相同的对象
ace_h = Cards("Ace", "Hearts", 14)
two_h = Cards("Two", "Hearts", 2)
three_h = Cards("Three", "Hearts", 3)
cards = [ace_h, two_h, three_h]
print(cards)
for card in cards:
print(id(card))
print(id(ace_h))
print(id(two_h))
print(id(three_h))
random_card = random.choice(cards)
print(id(random_card))
print(random_card.face)
print('random card is ace_h? {}'.format(random_card is ace_h))
print('random card is two_h? {}'.format(random_card is two_h))
print('random card is three_h? {}'.format(random_card is three_h))
如果您只是将变量看作附加到值上的标签,您的境况会好得多。在您的情况下,您正在创建卡
值,并将标签粘贴在卡上。您还将该卡
放入一个列表中,您可以通过名称卡[0]
来引用它。你可以给它取任意数量的名字:
another_name = ace_h
cool_name = ace_h
king_arthur = ace_h
sir_lancelot = ace_h
minister_of_silly_walks = ace_h
现在,这些名称中的每一个都引用了完全相同的值。您可以使用
id
进行检查,您可以看到它们都具有print(dir(ace_h))
和print(dir(dir(minister_of_dully_walks))
的正确属性。您可以检查它们是否是与ace\h is minister\u of u dully\u walks
相同的对象。您对如何在Python中引用内容有一个基本的误解
在Python中,有变量名:
ace_h = 1
two_h = 2
three_h = 3
您可以按名称调用这些值:
print(ace_h) # => 1
您可以将这些值放入另一个集合:
cards = [ace_h, two_h, three_h]
print(cards) # [1, 2, 3]
可以打印特定元素:
print(cards[0])
可以打印随机元素:
print(random.choice(cards))
但这些都是指值1、2、3
通过检查id
,您实际上可以看到:
print(id(ace_h))
print(id(cards[0]))
无论您是使用ace\u h
还是cards[0]
方法来命名值,都是相同的
这对Python中的所有东西都是一样的——它们都是对象。你可以给他们任何类型的名字,不管是变量名,把他们放在列表中,还是放在字典里——只要id(thing)
是相同的,你谈论的是完全相同的对象
ace_h = Cards("Ace", "Hearts", 14)
two_h = Cards("Two", "Hearts", 2)
three_h = Cards("Three", "Hearts", 3)
cards = [ace_h, two_h, three_h]
print(cards)
for card in cards:
print(id(card))
print(id(ace_h))
print(id(two_h))
print(id(three_h))
random_card = random.choice(cards)
print(id(random_card))
print(random_card.face)
print('random card is ace_h? {}'.format(random_card is ace_h))
print('random card is two_h? {}'.format(random_card is two_h))
print('random card is three_h? {}'.format(random_card is three_h))
如果您只是将变量看作附加到值上的标签,您的境况会好得多。在您的情况下,您正在创建卡
值,并且正在粘贴标签ace_h