Python: exec() 和对象列表
Python: exec() and lists of objects
我正在尝试创建一个目前仅包含以下内容的动态函数:
"chosen = random.choice([<ClassLibrary.Field object at 0x0330FA48> , <ClassLibrary.Field object at 0x0330FAA8> , <ClassLibrary.Field object at 0x0330FB08> , <ClassLibrary.Field object at 0x0330FB98> , <ClassLibrary.Field object at 0x0330FC28> , <ClassLibrary.Field object at 0x0330FCB8> , <ClassLibrary.Field object at 0x0330FD48> , <ClassLibrary.Field object at 0x0330FDD8> , <ClassLibrary.Field object at 0x0330FE68>])"
意思是我有一个变量,它被设置为等于列表中的一个随机元素。但是我收到一个语法错误,大概是因为列表对象是对内存中那些对象的 space 的引用? choice() 像这样处理原始列表:
chosen = random.choice(ticTacToe.fields)
我得到的错误是:
File "C:\Users\churc\Documents\P7\P7 Modular Playtesting\ticTacToe2.py", line 267, in <module>
eval(FunctionLibrary.makeExecutableStatement(ticTacToe.players[1].actions[0].statements[0]))
File "<string>", line 1
chosen = random.choice([<ClassLibrary.Field object at 0x0330FA48> , <ClassLibrary.Field object at 0x0330FAA8> , <ClassLibrary.Field object at 0x0330FB08> , <ClassLibrary.Field object at 0x0330FB98> , <ClassLibrary.Field object at 0x0330FC28> , <ClassLibrary.Field object at 0x0330FCB8> , <ClassLibrary.Field object at 0x0330FD48> , <ClassLibrary.Field object at 0x0330FDD8> , <ClassLibrary.Field object at 0x0330FE68>])
^
SyntaxError: invalid syntax
[Finished in 0.1s with exit code 1]
有人知道我做错了什么吗?以及如何纠正它?
<ClassLibrary.Field object at 0x0330FA48>
是无效的 python 语法,因为它只是 对象实例 的输出字符串。没有足够的信息来提供关于您的代码应该做什么的完整答案,但从您所写的内容来看,我建议在您之前声明的 ClassLibrary.Field
实例列表中使用 random.choice()
。
示例:
instances_list = [ ClassLibrary.Field() for instance in range(4) ]
chosen = random.choice(instances_list)
我正在尝试创建一个目前仅包含以下内容的动态函数:
"chosen = random.choice([<ClassLibrary.Field object at 0x0330FA48> , <ClassLibrary.Field object at 0x0330FAA8> , <ClassLibrary.Field object at 0x0330FB08> , <ClassLibrary.Field object at 0x0330FB98> , <ClassLibrary.Field object at 0x0330FC28> , <ClassLibrary.Field object at 0x0330FCB8> , <ClassLibrary.Field object at 0x0330FD48> , <ClassLibrary.Field object at 0x0330FDD8> , <ClassLibrary.Field object at 0x0330FE68>])"
意思是我有一个变量,它被设置为等于列表中的一个随机元素。但是我收到一个语法错误,大概是因为列表对象是对内存中那些对象的 space 的引用? choice() 像这样处理原始列表:
chosen = random.choice(ticTacToe.fields)
我得到的错误是:
File "C:\Users\churc\Documents\P7\P7 Modular Playtesting\ticTacToe2.py", line 267, in <module>
eval(FunctionLibrary.makeExecutableStatement(ticTacToe.players[1].actions[0].statements[0]))
File "<string>", line 1
chosen = random.choice([<ClassLibrary.Field object at 0x0330FA48> , <ClassLibrary.Field object at 0x0330FAA8> , <ClassLibrary.Field object at 0x0330FB08> , <ClassLibrary.Field object at 0x0330FB98> , <ClassLibrary.Field object at 0x0330FC28> , <ClassLibrary.Field object at 0x0330FCB8> , <ClassLibrary.Field object at 0x0330FD48> , <ClassLibrary.Field object at 0x0330FDD8> , <ClassLibrary.Field object at 0x0330FE68>])
^
SyntaxError: invalid syntax
[Finished in 0.1s with exit code 1]
有人知道我做错了什么吗?以及如何纠正它?
<ClassLibrary.Field object at 0x0330FA48>
是无效的 python 语法,因为它只是 对象实例 的输出字符串。没有足够的信息来提供关于您的代码应该做什么的完整答案,但从您所写的内容来看,我建议在您之前声明的 ClassLibrary.Field
实例列表中使用 random.choice()
。
示例:
instances_list = [ ClassLibrary.Field() for instance in range(4) ]
chosen = random.choice(instances_list)