Created
January 24, 2014 07:46
-
-
Save dmikurube/8593604 to your computer and use it in GitHub Desktop.
Importing a file into a class dynamically
This file contains hidden or 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
text = 'Test 1' | |
def print_text(): | |
print text |
This file contains hidden or 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
text = 'foobar' | |
def print_text(): | |
print text |
This file contains hidden or 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
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