python use bound method of a class created by string variable -
i have possible stupid question regarding python forgive me in advance. have following class in file check.py in folder checks:
class check(object): def __init__(self): print "i initialized" def get_name(): return "my name check"
and class loaded through variable string following function:
def get_class( kls ): parts = kls.split('.') module = ".".join(parts[:-1]) m = __import__( module ) comp in parts[1:]: m = getattr(m, comp) return m
i have reasons create class defined string variable don't try circumvent this.
now when run following:
from checks import * # __init__.py correct of 1 s="check" cl=get_class("checks."+s+"."+s.title()) a=cl() print str(a) print "name="+str(a.get_name)
i following output:
i initialized <checks.check.check object @ 0x0000000002db8940> name=<bound method check.get_name of <checks.check.check object @ 0x0000000002db8940>>
now question: there way can access check.get_name method? can result "my name check"?
`
you need change check.get_name
definition:
class check(object): def __init__(self): print "i initialized" def get_name(self): return "my name check"
then can access following code:
from checks import * # __init__.py correct of 1 s="check" cl=get_class("checks."+s+"."+s.title()) a=cl() print a.get_name()
Comments
Post a Comment