-
Notifications
You must be signed in to change notification settings - Fork 0
/
66_Alternative_constructors.py
25 lines (18 loc) · 1.21 KB
/
66_Alternative_constructors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"""
Class Methods as Alternative Constructors
---
In object-oriented programming, the term "constructor" refers to a special type of method that is automatically executed when an object is created from a class. The purpose of a constructor is to initialize the object's attributes, allowing the object to be fully functional and ready to use.
However, there are times when you may want to create an object in a different way, or with different initial values, than what is provided by the default constructor. This is where class methods can be used as alternative constructors.
A class method belongs to the class rather than to an instance of the class. One common use case for class methods as alternative constructors is when you want to create an object from data that is stored in a different format, such as a string or a dictionary.
"""
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def from_string(cls, string):
name, age = string.split(',')
return cls(name, int(age))
a1 = Person("Omprakash Prajapati", 22) # Normal initialization
a2 = Person.from_string("Paras Shewale, 22") # Initialization with different values
print(a2.name)