Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python constructor overloading

class MyData:
    def __init__(self, data):
        self.data = data

    @classmethod
    def fromfilename(cls, filename):
        data = open(filename).readlines()
        return cls(data)

    @classmethod
    def fromdict(cls, datadict):
        MyData([1, 2, 3]).data
        return cls(datadict.items())


print(MyData.fromfilename("file.txt").data)

print(MyData.fromdict({"spam": "ham"}).data)

# ** Output: **
#[‘Welcome Developer’]
#dict_items([(‘spam’, ‘ham’)])
Source by tutorial.eyehunts.com #
 
PREVIOUS NEXT
Tagged: #python #constructor #overloading
ADD COMMENT
Topic
Name
7+6 =