简体   繁体   English

对象列表不可迭代,并且调用变量在其他函数中

[英]List of objects is not iterable & call variable being in other function

I have seen this post but still can not figure out my problem. 我看过这篇文章,但仍然无法解决我的问题。 Python iteration of list of objects "not iterable" My question also has a second part which is: How can I use variable which was created in one function in other. 对象列表的Python迭代“不可迭代”我的问题还包含第二部分: 如何使用在另一个函数中创建的变量。 I will start with it. 我将从它开始。 Answers that I googled or fined in SO: 我用google搜索或罚款的答案:

1 variant - globals 1个变量-全局变量

def func1():
    global value2,value1
    value1 = 1
    value2 = 2
def func2():
    global value2,value1
    do smth with value1 
    do smth with value2

func1()
func2()

But this is bad idea, due to some reason. 但这是一个坏主意,由于某种原因。 I do not get still too much, like I am using same namespaces, and limit variants of naming variables. 我不会得到太多,就像我使用相同的名称空间一样,并限制命名变量的变体。 And some unpredictable behavior is much easier to get..(that is what I understood) 而且一些难以预测的行为更容易得到..(这是我的理解)

2 variant -functions are objects, and everything in Python is an object! 2个变体-函数是对象,Python中的所有对象都是对象!

def func1():
    func1.value1 = 1
    func1.value2 = 2
def func2():
    do smth with func1.value1
    do smth with func1.value2

func1()
func2()

This is a better idea? 这是一个更好的主意吗?

3 variant - use return 3个变体-使用退货

I know that everybody says : try to make your function to return some value which you will use further. 我知道每个人都说:尝试使您的函数返回一些值,您将在以后使用它们。 But! 但! I have a function draw() which is rerunning 60 times per second, that is why writing like this is a mess: 我有一个函数draw()每秒重新运行60次,这就是为什么这样写很烂:

def draw():
    variable = some_function()

My function will be starting 60times per second, where I need just 1 time till specific trigger/button is pressed. 我的功能是每秒启动60次,我只需要1次就可以按下特定的触发器/按钮。

Finally my main question Here is my code: 最后,我的主要问题是我的代码:

class Card:
    def __init__(self, suit, rank):
        ...
    def draw(self, canvas, pos):
        ...
class Hand:
    def __init__(self):
        self.hand_list = []
    def draw(self, canvas, pos):
        ...
    deff add_card(self)

def deal():
    test_hand = Deck() # from the Deck we fulfill cards to player and computer hands
    test_hand.shuffle()
    deal.player_hand, computer_hand = Hand(), Hand()

def draw(canvas):
    i = 0
    for card in deal.player_hand:
        deal.player_hand.draw(canvas, [300, 300])

And finnaly I get. 最后,我明白了。

TypeError: 'Hand' object is not iterable TypeError:“手”对象不可迭代

Somewhere type of self.hand_list changed from list to a non list. self.hand_list的某处类型从列表更改为非列表。 Or a problem is somewhere deeper. 或者问题更深层次。 Because when I look at what interpreter is printing - he is printing 2 Cards of some suit and rank as 1 object. 因为当我看口译员在打印什么时,他在打印2张具有某些西装的证件,并列为1个对象。 I do not know where to start from. 我不知道从哪里开始。 I would be really helpful for advices. 我将对您的建议真的很有帮助。

I have got Learning Python by Mark Lutz, maybe there are some topics should look deeper at? 我有Mark Lutz的《学习Python》,也许有一些主题需要深入研究?

Thanks everybody! 谢谢大家!

Here is the link to my code. 这是我的代码的链接。 You can run it from there. 您可以从那里运行它。 http://www.codeskulptor.org/#user35_zXdT3D8RpqJXoVb.py http://www.codeskulptor.org/#user35_zXdT3D8RpqJXoVb.py

Your problem is in the draw method: 您的问题出在draw方法中:

for card in deal.player_hand:
    ...

You're iterating over player_hand , which is an object of the Hand class, and not the cards within that hand. 您正在遍历player_hand ,后者是Hand类的一个对象,而不是该手中的纸牌。 You probably want to do for card in deal.player_hand.hand_list . 您可能想要for card in deal.player_hand.hand_list

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

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