Click here to Skip to main content
14,924,426 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I would like to split the above into 3 modules, so the name of the class
and the attributes cna be changed using variables.

Write the import statements for modules A, B and C

Python
#A.py - create a class in  module A 
class Comp
    Name  = ''
    Type  = ''

#B.py - instantiate Comp in module B
def Inst(Var1, Var2):
    Var1 = Comp
    Var1.Name = Var1
    Var1.Type = Var2
    
#C.py - supply the variables from module C    
Inst('Bob', 'Tall')
print(Bob.Name, Bob.Type)
>> Bob Tall


What I have tried:

Python
<pre>class Comp:
    Name = ''
    Type = ''
    
Var1 = 'Bob'
Var2 = 'Tall'

exec(Var1 + ' = ' + 'Comp')
exec(Var1 + '.' + 'Name' + ' = ' + "'" + Var1 + "'")
exec(Var1 + '.' + 'Type' + ' = ' + "'" + Var2 + "'")

print(Bob.Name, Bob.Type)
>> Bob Tall    
Posted
Comments
Richard MacCutchan 8-Oct-20 3:53am
   
You cannot use strings as variable names.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900