Skip to content

Instantly share code, notes, and snippets.

@fiee
Created April 12, 2016 09:46
Show Gist options
  • Save fiee/ccea234f1ac12a9280b631e474fe5662 to your computer and use it in GitHub Desktop.
Save fiee/ccea234f1ac12a9280b631e474fe5662 to your computer and use it in GitHub Desktop.
get a Python class from a class name
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])
@fiee
Copy link
Author

fiee commented Apr 12, 2016

Because I was searching quite long for a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment