Questions And Answers

More Tutorials

Python Class Methods As Alternative Constructors

The parameters that we have to pass to our constructor would be the class i.e., cls and the string containing the parameters. Moving on towards the working, we have to use a function "split()," that will divide the string into parts. And the parts as results will be stored in a list. We can now pass the parameters to the constructor using the index numbers of the list or by the concept of *args .


It takes a separator as a parameter. If we do not provide any, then the default separator is any whitespace it encounters. Else we can provide any separator to it such as full stop, hash, dash, colon, etc. After separating the string into parts, the split() function stores it into a list in a sequence. For example:

text = "Python tutorial for absolute beginners."
 t = text.split()

Here, we are not providing it any separator as a parameter, so it will automatically divide, taking whitespace as a separator.

The output will be a list, such as:

['Python', 'tutorial', 'for', 'absolute', 'beginners.']

Example of Class methods - alternative constructor:

class Date:
    def __init__(self, year, month, day):
        self.year = year
        self.month = month = day

    def from_dash(cls,string):
          return cls(*string.split("-"))

#Output: 2008

If we want multiple and independent "constructors", we can use class methods. They are usually called factory methods. It does not invoke the default constructor __init__.In the above example, we split the string based on the "-" operator. We first create a class method as a constructor that takes the string and split it based on the specified operator. For this purpose, we use a split() function, which takes the separator as a parameter. This alternative constructor approach is useful when we have to deal with files containing string data separated by a separator.


class Employee:
    no_of_leaves = 8

    def __init__(self, aname, asalary, arole): = aname
        self.salary = asalary
        self.role = arole

    def printdetails(self):
        return f"The Name is {}. Salary is {self.salary} and role is {self.role}"

    def change_leaves(cls, newleaves):
        cls.no_of_leaves = newleaves

    def from_dash(cls, string):
        return cls(*string.split("-"))

harry = Employee("Harry", 255, "Instructor")
rohan = Employee("Rohan", 455, "Student")
karan = Employee.from_dash("Karan-480-Student")



In this page (written and validated by ) you learned about Python Class Methods As Alternative Constructors . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Static Methods.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.