I'm trying to name an instance with the result of input() function in python. How can I make variable/instance name with string?
I've found exec() function and tried it, but get syntax error. I don't get why it happens.
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
you don't have to take close look at all these class description but I just appended it to explain how my "Expression" class looks like.
(all these right side would be gained by input() function)
form = 'von'
pos = 'p'
agr = None
spr = 'underspecified'
comps = 'NP_3'
exec('{} = {}'.format(form, Expression(form, "word", Head(pos, agr), Val(spr, comps))))
this is what I tried to make it.
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
this is what I get from the code above.
my hopeful result would be
von = Expression('von','word',Head('p',None),Val('underspecified','NP_3')