如何使用字符串命名实例?

How can I name an instance with a string?

我正在尝试用 python 中的 input() 函数的结果命名一个实例。如何用字符串创建 variable/instance 名称?

我找到了 exec() 函数并尝试了它,但出现语法错误。我不明白为什么会这样。

class Expression:
    def __init__(self, form, sort, head, val):
        self.form = form
        self.sort = sort
        self.head = head
        self.val = val
class Head(Expression):
    def __init__(self, pos, agr):
        self.pos = pos
        self.agr = agr
        agr_pos = ['n', 'd', 'v', 'a', 'pn']
        if self.pos not in agr_pos:
        self.agr = None
class Agr(Head):
    def __init__(self, agr_info):
        self.per = agr_info[0]
        self.num = agr_info[1]
        self.gen = agr_info[2]
        self.case= agr_info[3]
        self.det = agr_info[4]
        self.svagr = self.per + self.num + self.case
        self.npagr = self.num + self.gen + self.case + self.det
class Val(Expression):
    def __init__(self, spr, comps):
        self.spr = spr
        self.comps = comps

您不必仔细查看所有这些 class 描述,但我只是附加它来解释我的 "Expression" class 的样子。

(所有这些右侧都将通过 input() 函数获得)

form = 'von'
pos = 'p'
agr = None
spr = 'underspecified'
comps = 'NP_3'
exec('{} = {}'.format(form, Expression(form, "word", Head(pos, agr), Val(spr, comps))))

这是我努力做到的。

Traceback (most recent call last):
  File "test.py", line 37, in <module>
    exec('{} = {}'.format(form, Expression(form, "word", Head(pos, agr), 
Val(spr, comps))))
  File "<string>", line 1
    von = <__main__.Expression object at 0x01080F10>
          ^
SyntaxError: invalid syntax

这是我从上面的代码中得到的。

我希望的结果是

von = Expression('von','word',Head('p',None),Val('underspecified','NP_3')

首先,您在 类、class Head(Expression):class Agr(Head): 等中使用继承,但您没有调用 super().__init__ 来实例化 super类。所以我假设你不小心使用了它们,我已经删除了它们

评论者@Barmar 和@KlausD 也提到了这一点。上面,使用exec赋值变量是个坏主意,直接使用变量赋值总是更好。

此外,您正在使用的 exec 语句的计算结果为 von = <__main__.Expression object at 0x10367e9b0>,因为右侧打印了对象 <__main__.Expression object at 0x10367e9b0>str 表示,您无法将其分配给变量。

做出这些假设,您的代码将更改为。

class Expression:
    def __init__(self, form, sort, head, val):
        self.form = form
        self.sort = sort
        self.head = head
        self.val = val

#Removed the superclass reference
class Head:
    def __init__(self, pos, agr):
        self.pos = pos
        self.agr = agr
        agr_pos = ['n', 'd', 'v', 'a', 'pn']
        if self.pos not in agr_pos:
            self.agr = None

#Removed the superclass reference
class Agr:
    def __init__(self, agr_info):
        self.per = agr_info[0]
        self.num = agr_info[1]
        self.gen = agr_info[2]
        self.case= agr_info[3]
        self.det = agr_info[4]
        self.svagr = self.per + self.num + self.case
        self.npagr = self.num + self.gen + self.case + self.det

#Removed the superclass reference
class Val:

    def __init__(self, spr, comps):
        self.spr = spr
        self.comps = comps

form = 'von'
pos = 'p'
agr = None
spr = 'underspecified'
comps = 'NP_3'
#Used variable assignment here instead of exec and used a new variable expr
expr = Expression(form, "word", Head(pos, agr), Val(spr, comps))```