0

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')
secondrun
  • 11
  • 2

1 Answers1

0

To begin with, You are using inheritance in your classes, class Head(Expression): , class Agr(Head): etc, but you are not calling super().__init__ to instantiate the superclasses. So I assume you used them accidentally, and I have removed them

Also as mentioned by the commentors @Barmar and @KlausD. above, using exec to assign variables is a bad idea, it is always better to use variable assignment directly.

Also the exec statement you are using evaluates to von = <__main__.Expression object at 0x10367e9b0> because the right side prints the str representation of the object <__main__.Expression object at 0x10367e9b0> , which you cannot assign to a variable.

Making these assumptions, your code will change to.

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))``` 
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
  • thought it'd be better if I can handle those dynamic variables, but seems that's not quite right. thx for your help~ – secondrun May 04 '19 at 08:57
  • No problem! We have data structures like lists and dictionaries to avoid these issues! Also it would be great if the answer helped you, if you could mark it as accepted @secondrun :) – Devesh Kumar Singh May 04 '19 at 09:00