type

A class defines the properties and available actions of its object and also it acts as a factory for object creation. Let’s understand the process by creating a class using type directly. The exact class that is used for class instantiation is called type. Normally, we define a class using a special syntax called the class keyword, but this syntax is a substitute for type class. Let’s illustrate with an example:

 First of all, we look into the scenario of creating a class using the class keyword. Let’s check the below code:

Python3




class FoodType(object):
  def __init__(self, ftype):
    self.ftype = ftype
      
  def getFtype(self):
    return self.ftype
    
  
def main():
  fType = FoodType(ftype = 'Vegetarian')
  print(fType.getFtype())
    
main()


Output

Vegetarian



Here we have created a class called FoodType using the class keyword. This class keyword acts as a substitute for type syntax. Now let’s look into, how to use type keyword. Let’s go through the below code:

Python3




def init(self, ftype):
    self.ftype = ftype
  
def getFtype(self):
    return self.ftype 
  
FoodType = type('FoodType', (object, ), {
    '__init__': init,
    'getFtype' : getFtype,
    })
  
fType = FoodType(ftype ='Vegetarian')
print(fType.getFtype())


Output

Vegetarian



Let’s focus on type. It has three arguments they are as follows:

  • The first argument is a string – FoodType. This string is assigned as the class name.
  • The second argument is a tuple – (object, ). This tells that the FoodType class inherits from the object class. Here, the trailing comma helps the python interpreter to recognize it as a tuple.
  • Here, the third argument is a dictionary that mentions the attribute of a class. In this case, the class has two methods – init and getFtype.

Creating a Subclass using type

Let’s look into the normal scenario of creating a subclass, i.e., using the class keyword. Here, we will create a subclass VegType in which the main class is FoodType.

Python3




class FoodType(object):
  def __init__(self, ftype):
    self.ftype = ftype
      
  def getFtype(self):
    return self.ftype
    
class VegType(FoodType):
  def vegFoods(self):
    return {'Spinach', 'Bitter Guard'}
    
def main():
  vType = VegType(ftype = 'Vegetarian')
  print(vType.getFtype())
  print(vType.vegFoods())
    
main()


Output

Vegetarian
{'Spinach', 'Bitter Guard'}



Now let’s see how to convert the above code using type.  For the FoodType class, the second argument of the type is the object class –  the superclass –, i.e., FoodType is the subclass of the Object class. Similarly, the VegType class is the subclass of FoodType, and hence while creating the VegType class, the second argument of type refers to the FoodType class. 

Python3




def init(self, ftype):
    self.ftype = ftype
  
def getFtype(self):
    return self.ftype 
  
FoodType = type('FoodType', (object, ), {
    '__init__': init,
    'getFtype' : getFtype,
    })
  
def vegFoods(self):
    return {'Spinach', 'Bitter Guard'}
   
## creating subclass using type
VegType = type('VegType', (FoodType, ), {
    'vegFoods' : vegFoods,
    })
  
  
vType = VegType(ftype ='Vegetarian')
print(vType.getFtype())
print(vType.vegFoods())


Output

Vegetarian
{'Spinach', 'Bitter Guard'}



Python MetaClasses

The key concept of python is objects. Almost everything in python is an object, which includes functions and as well as classes. As a result, functions and classes can be passed as arguments, can exist as an instance, and so on. Above all, the concept of objects let the classes in generating other classes.

The classes that generate other classes are defined as metaclasses. In this section, we will discuss the concept of metaclasses and the specific ways to use them. In this section, we will cover the following topics:

  • type
  • Writing Metaclasses
  • Metaclass Usecases

Similar Reads

type

A class defines the properties and available actions of its object and also it acts as a factory for object creation. Let’s understand the process by creating a class using type directly. The exact class that is used for class instantiation is called type. Normally, we define a class using a special syntax called the class keyword, but this syntax is a substitute for type class. Let’s illustrate with an example:...

Writing Metaclasses

...

Metaclass Usecases

...

Summary

...