Skip to content

Instantly share code, notes, and snippets.

@dmikurube
Created January 24, 2014 07:46
Show Gist options
  • Save dmikurube/8593604 to your computer and use it in GitHub Desktop.
Save dmikurube/8593604 to your computer and use it in GitHub Desktop.
Importing a file into a class dynamically
text = 'Test 1'
def print_text():
print text
text = 'foobar'
def print_text():
print text
class Importer(object):
def __init__(self, importee):
_tmp_module = __import__(importee)
for name in dir(_tmp_module):
if (not name.startswith('__')) or (not name.endswith('__')):
setattr(self, name, getattr(_tmp_module, name))
k1 = Importer('importee1')
k1.print_text()
k2 = Importer('importee2')
k2.print_text()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment