Python Type - Dynamic Class

Card Puncher Data Processing


With the three arguments of the type function, you can create dynamically a new class (ie a type object).

type is a class that creates class objects

<class 'type'>


class type(name, bases, dict)


class MyMinimalClass(object):
type('MyMinimalClass', (), {})


Create Attribute (variable, function)

The following two statements from the doc create two identical type objects with two attributes:

  • one as a variable
  • an other as function

This code:

class X(object):
        a = 1;
        def echo(self):

is identical with the below one.

def echo_a(self):

X = type('X', (object,), dict(a=1,echo_a=echo_a))
# or
X = type('X', (object,), { 'a':1, 'echo_a': echo_a } )

Create a class form a parent (Inheritance)

Python - Class Inheritance

FooChild = type('FooChild', (Foo,), {})


  • Foo is the parent class
  • FooChild is the child class

Documentation / Reference

Discover More
Card Puncher Data Processing
Object Type - Class

The type of an object is its class. See
Card Puncher Data Processing
Python - Class

in Python The difference with a standard class implementation is that in Python, Classes are objects because the class keyword will creates an object. Only a class object is capable of creating objects. A...
Card Puncher Data Processing
Python - Class Inheritance

in Python where: the derived class is the new class and the base class is the class from which that new class inherits. See To directly access the attributes or methods of a superclass,...

Share this page:
Follow us:
Task Runner