Created
April 12, 2016 09:46
-
-
Save fiee/ccea234f1ac12a9280b631e474fe5662 to your computer and use it in GitHub Desktop.
get a Python class 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
import importlib | |
def class_from_name(name): | |
""" | |
Take a class name like `django.db.models.Model` and return the class | |
""" | |
parts = name.split('.') | |
mod = importlib.import_module('.'.join(parts[:-1])) | |
return getattr(mod, parts[-1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because I was searching quite long for a solution.