Created
April 2, 2012 09:06
-
-
Save robinedwards/2281914 to your computer and use it in GitHub Desktop.
creating an object from a class name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pprint import pprint as pp; | |
import sys | |
class Foo(object): | |
def __init__(self, name): | |
print "building a Foo" | |
self.name = name | |
def sayhi(self): | |
print "Hi! I am ", self.name | |
myfooclass = getattr(sys.modules[__name__], 'Foo') | |
print "myfooclass: ", myfooclass | |
a = myfooclass("Jim") | |
a.sayhi(); | |
print "a: ", a | |
# Output: | |
myfooclass: <class '__main__.Foo'> | |
building a Foo | |
Hi! I am Jim | |
a: <__main__.Foo object at 0x1603390> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment