Python Anonymous Classes and Objects: Dynamic Object Creation

thumb_up 1  ·  sell Python anonymous classes and objects, Dynamic object creation in Python, Creating nameless classes and objects in Python

Python's built-in type() function returns the class that an object belongs to. In Python, a class, both a built-in class or a user-defined class are objects of type class.

Example

 
class myclass: def __init__(self): self.myvar=10 return obj = myclass() print ('class of int', type(int)) print ('class of list', type(list)) print ('class of dict', type(dict)) print ('class of myclass', type(myclass)) print ('class of obj', type(obj))

It will produce the following output −

class of int <class 'type'>
class of list <class 'type'>
class of dict <class 'type'>
class of myclass <class 'type'>

The type() has a three argument version as follows −

Syntax

newclass=type(name, bases, dict)

Using above syntax, a class can be dynamically created. Three arguments of type function are −

  • name − name of the class which becomes __name__ attribute of new class

  • bases − tuple consisting of parent classes. Can be blank if not a derived class

  • dict − dictionary forming namespace of the new class containing attributes and methods and their values.

We can create an anonymous class with the above version of type() function. The name argument is a null string, second argument is a tuple of one class the object class (note that each class in Python is inherited from object class). We add certain instance variables as the third argument dictionary. We keep it empty for now.

anon=type('', (object, ), {})

To create an object of this anonymous class −

obj = anon() print ("type of obj:", type(obj))

The result shows that the object is of anonymous class

type of obj: <class '__main__.'>

Example

We can also add instance variables and instance methods dynamically. Take a look at this example −

 
def getA(self): return self.a obj = type('',(object,),{'a':5,'b':6,'c':7,'getA':getA,'getB':lambda self : self.b})() print (obj.getA(), obj.getB())

It will produce the following output −

5 6



The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back