Skip to content

Instantly share code, notes, and snippets.

@robinedwards
Created April 2, 2012 09:06
Show Gist options
  • Save robinedwards/2281914 to your computer and use it in GitHub Desktop.
Save robinedwards/2281914 to your computer and use it in GitHub Desktop.
creating an object from a class name
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