0

Below is my code requirement :

Class Employee:
    def __init__(self,age):
        self.age= age

main():

Mike = new Employee(20)
NamedEmployee = "Mike"

# Need some logic to refer Mike Object with value given in NamedEmployee 
string
# example
print(NamedEmployee.age) -- expect 20 here.

Need some insights in how to achieve this in python.

Kenster
  • 23,465
  • 21
  • 80
  • 106
kaushik H S
  • 129
  • 13

1 Answers1

0

If you want Namedemployee to be uniquely defined by Mike then you can use

    Class Employee:
        def __init__(self,age):
            self.age= age

    Mike = Employee(20)
    NamedEmployee = eval("Mike")
    print(NamedEmployee.age) # print 20 

if you want to have a category of, define a dict like :

    NamedEmployee = dict() 
    NamedEmployee['Mike'] = eval("Mike")

Note: becareful with eval() due to security issues , see Security of Python's eval() on untrusted strings?