Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
class first:
def identify( self ):
print "first"
import myclass
inst = myclass.first()
inst.identify()
import myclass
import indirection
inst = indirection.myclass.first()
inst.identify()
import indirection
classname = "indirection.myclass.first"
def forName( fullname ):
# I wish I didn't have to treat the top level
# differently, maybe someone more familiar
# with namespaces could normalize this
parts = fullname.split( "." )
object = globals()[ parts[0] ]
# Now that we have a module...
for part in parts[1:]:
object = getattr( object, part )
# we've got it, so instantiate it
return object()
# find it...
instance = forName( classname )
# prove it...
instance.identify()
>>> class A: pass
...
>>> s = "A"
>>> b = eval(s)()
>>> b
<__main__.A instance at 0x01435FD0>
>>>
# map strings to corresponding classes
parserClassMap = {
"FileType1" : File1Parser,
"FileType2" : File2Parser,
}
fileType = raw_input("Type of file:")
if fileType in parserClassMap:
# invoke constructor
parser = parserClassMap[ fileType ]()
else:
print "unrecognized file type"