
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How we can instantiate different python classes dynamically?
To instantiate the python class, we need to get the class name first. This is achieved by following code
def get_class( kls ): parts = kls.split('.') module = ".".join(parts[:-1]) m = __import__( module ) for comp in parts[1:]: m = getattr(m, comp) return m
m is the class
We can instantiate this class as follows
a = m() b = m(arg1, arg2) # passing args to the constructor
- Related Articles
- How we can extend multiple Python classes in inheritance?
- How do we use different CSS classes in HTML?
- How I can dynamically import Python module?
- How can Tensorflow be used to instantiate an estimator using Python?
- How can we change the JButton text dynamically in Java?\n
- How do we handle circular dependency between Python classes?
- How do we use equivalence (“equality”) operator in Python classes?
- How to discriminate between different classes?
- How can we use a diamond operator with anonymous classes in Java 9?
- How can we obtain different gases from the air?
- How can we apply different borders to JButton in Java?
- How can we find underroot or squareroot of different numbers ?
- How to dynamically load a Python class?
- How we can bundle multiple python modules?
- How can we overload a Python function?

Advertisements