Python Constructeur surcharge
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’)])
SharxNZ