When you should write Class Factories
Let’s have a look at some of the use cases of class factories. Class Factories are useful when you do not know what attributes to be assigned at the time of coding
Runtime Attributes
Class Factories are necessary when attributes of the class differ based on the requirement. Let’s consider the case of a login process. Here, we will consider two scenarios, either traditional login or using an OpenId service. If we look into traditional login, the parameters are username and password, and additionally, it may have two-factor authentication. And, for OpenId service, the parameters are service name and email address. This two login scenario points to the fact that attributes of a class differ based on the login service. Let’s look in the below sample code:
Python3
def credentials_cls(need_proxy = False , tfa = False ): # need proxy for openId services if need_proxy: print ( "Open Id Service" ) keys = [ 'service_name' , 'email_address' ] else : print ( "Traditional Login" ) keys = [ 'username' , 'password' ] # two factor authentication for traditional login if tfa: keys.append( 'auth_token' ) class CredentialCheck( object ): required_keys = set (keys) def __init__( self , * * kwargs): # checking whether key matches based on login service if self .required_keys ! = set (kwargs.keys()): raise ValueError( 'Mismatch' ) # storing the keys and values to the credential object for k, v in kwargs.items(): setattr ( self , k, v) return CredentialCheck CredCheck = credentials_cls( False , False ) crdTraditional = CredCheck(username = 'uname' , password = '******' ) OpenIDCheck = credentials_cls( True , False ) crdOpenID = OpenIDCheck(service_name = 'sname' , email_address = 'email@gmail.com' ) |
Traditional Login Open Id Service
Modify Class Attributes
Another advantage of using class attributes is that it can deal with class attributes and can distinguish them from class instances. Let’s consider the scenario where a class defines a class method. Class methods are methods that require the class itself for execution rather than the instance of a class. You can design a class method by decorating a method using @classmethod decorator. Let’s look at the below code.
Python3
class Apple( object ): color = 'red' @classmethod def classapple( cls ): return cls .color appleRed = Apple() appleYellow = Apple() appleGreen = Apple() print ( "Apple Red: " , appleRed.classapple()) appleYellow.color = 'Yellow' print ( "Apple Yellow: " , appleYellow.classapple()) appleGreen.color = 'Green' print ( "Apple Green: " , appleGreen.classapple()) |
Apple Red: red Apple Yellow: red Apple Green: red
In the above code, we have designed a class called Apple that has color as an attribute. In addition to this, we have declared a class method called classapple using the decorator @classmethod. The functionality of classapple method is to return the color of the apple. But, you can note that even after setting the color of apple to Yellow and Green, the object returns the default color red. This limitation can be overcome using a class factory.
Let’s see the below code that defines a class factory called create_Apple_subclass. Here we will create a subclass of Apple, subApple, to set the color. Finally, the class factory returns the subclass.
Python3
class Apple( object ): color = 'red' @classmethod def classapple( cls ): return cls .color def create_Apple_subclass(new_color): class SubApple(Apple): color = new_color return SubApple sappleYellow = create_Apple_subclass( 'Yellow' ) print ( "Apple Color: " , sappleYellow.classapple()) sappleGreen = create_Apple_subclass( 'Green' ) print ( "Apple Color: " , sappleGreen.classapple()) |
Apple Color: Yellow Apple Color: Green
Class Factories: A powerful pattern in Python
A Class Factory is a function that creates and returns a class. It is one of the powerful patterns in Python. In this section, we will cover how to design class factories and the use cases of it.
Contact Us