首页 > 解决方案 > 如何用字符串命名实例?

问题描述

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

我找到了 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

您不必仔细查看所有这些类描述,但我只是附加它来解释我的“表达式”类的外观。

(所有这些右侧都将通过 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')

标签: pythonclasssyntax-errorexecinstance

解决方案


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

正如评论员@Barmar 和@KlausD 所提到的。上面,使用exec分配变量是一个坏主意,最好直接使用变量分配。

此外,您正在使用的 exec 语句评估为,von = <__main__.Expression object at 0x10367e9b0>因为右侧打印了strobject 的表示<__main__.Expression object at 0x10367e9b0>,您不能将其分配给变量。

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

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))``` 

推荐阅读