python - 如何用字符串命名实例?
问题描述
我正在尝试用 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')
解决方案
首先,您在类中使用继承class Head(Expression):
,class Agr(Head):
等等,但您没有调用super().__init__
实例化超类。所以我假设你不小心使用了它们,我已经删除了它们
正如评论员@Barmar 和@KlausD 所提到的。上面,使用exec
分配变量是一个坏主意,最好直接使用变量分配。
此外,您正在使用的 exec 语句评估为,von = <__main__.Expression object at 0x10367e9b0>
因为右侧打印了str
object 的表示<__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))```
推荐阅读
- powershell - Powershell Pandoc 批处理当前目录
- azure - Azure - 文件的 PGP/GPG 解密
- c# - C#:变体类型参数只能在接口或委托中声明?
- python - 无法在 django python shell 中检查配置文件模型
- angular - 以编程方式访问 Angular i18n 包
- git - 当我们在 git 中将一个分支移动到同一项目的另一个分支时,从后台内存的角度来看会发生什么......?
- ios - 使用 RxSwift 的 UICollectionView 数据绑定 - iOS
- html - 当其父级使用 flex 时,如何将按钮定位到页面的右侧?
- html - 特定宽度的内容左侧和右侧的水平线
- machine-learning - 计算机视觉和机器学习中特征描述符的解释