如果类在Python中存在,如何附加该类的属性值

如果类在Python中存在,如何附加该类的属性值,python,Python,我正在尝试制作一个特殊的棋盘游戏,在查找变量“to”和“play”中是否存在属性时遇到了一些问题 我觉得这个方法是一种很长很繁琐的检查类中是否存在属性的方法。是否有一种方法可以通过属性进行for循环检查,并附加那些存在的属性,然后继续那些不存在的属性 最后两行用于检查index_error_lst,查看这些属性中的任何数字是否大于3(即最大玩家数/最大牌堆数),并返回错误 谢谢 功能性方法如何: from itertools import product # ... index_error_

我正在尝试制作一个特殊的棋盘游戏,在查找变量“to”和“play”中是否存在属性时遇到了一些问题

我觉得这个方法是一种很长很繁琐的检查类中是否存在属性的方法。是否有一种方法可以通过属性进行for循环检查,并附加那些存在的属性,然后继续那些不存在的属性

最后两行用于检查index_error_lst,查看这些属性中的任何数字是否大于3(即最大玩家数/最大牌堆数),并返回错误


谢谢

功能性方法如何:

from itertools import product

# ...

index_error_lst = filter(
                      # Filter out not-found or None values
                      lambda value: value is not None, 

                      map(
                          # Will receive tuple (obj, attr)
                          # Attempt to get the value or fallback to None
                          lambda attr: getattr(attr[0], attr[1], None),

                          # Cartesian product of its arguments
                          product(
                              (play, to), 
                              ('player_no', 'discard_pile_no', 'build_pile_no')
                          )
                      )
                  )

# ...

if any(value > 3 for value in index_error_lst):
    return 0

前两个
if
具有相同的条件。。!是打字错误吗?若这些是自定义类,可能会给它们一个成员函数,用于生成并返回信息列表。所以你可以:
index\u error\u lst=play.get\u error\u list()
。看起来你可能对你的类做了一些有问题的设计决定。我建议要么编辑这个问题,要么删除并提出一个新问题,其中包括你的课程框架。问题的根源可能在于类设计,而不是这个特定方法的实现。
from itertools import product

# ...

index_error_lst = filter(
                      # Filter out not-found or None values
                      lambda value: value is not None, 

                      map(
                          # Will receive tuple (obj, attr)
                          # Attempt to get the value or fallback to None
                          lambda attr: getattr(attr[0], attr[1], None),

                          # Cartesian product of its arguments
                          product(
                              (play, to), 
                              ('player_no', 'discard_pile_no', 'build_pile_no')
                          )
                      )
                  )

# ...

if any(value > 3 for value in index_error_lst):
    return 0