Return an object using its name in python -


so, i've create global objects can accessed method. have return objects method this.

def get_my_obj  (obj_name):       if   obj_name == "abc":  return  abc     elif obj_name == "xyz":  return  xyz     elif obj_name == "def":  return  def 

is there simpler way achieve this? have hundreds of these objects.

through use of globals() in appropriate file, entirely possible achieve such evil act.

# file1.py  abc = 'foo' xyz = 'bar' dfe = 'baz'   def get_my_obj(obj_name):     return globals()[obj_name]   # file2.py  file1 import get_my_obj  print(get_my_obj('abc'))  # 'foo' 

globals() retrieves variables within current module. in case, may file executed.

can it? yes. it? no. business decide if should anyway? no.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -