Search
 
SCRIPT & CODE EXAMPLE
 

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’)])
Comment

PREVIOUS NEXT
Code Example
Python :: flask decoding base 64 image 
Python :: joining two lists in python 
Python :: how to show a frequency distribution based on date in python 
Python :: matplotlib subplots 
Python :: pow python 
Python :: pyplot python 
Python :: how to open cmd at specific size using python 
Python :: python print value and variable name 
Python :: extract email address using expression in django 
Python :: subtract number from each element in list python 
Python :: django queryset to form 
Python :: np.multiply 
Python :: unique list values python ordered 
Python :: copy files to a directory using text file 
Python :: labs fill ggplot2 
Python :: pandas data frame to list 
Python :: geopandas stack or concatenate dataframe together 
Python :: open excel through python 
Python :: how to convert python input to int 
Python :: power function python 
Python :: get every item but the last item of python list 
Python :: pandas xa0 
Python :: how to get last n elements of a list in python 
Python :: remove in list python 
Python :: split by several characters python 
Python :: how to send file in python request 
Python :: python web crawler 
Python :: use the index of a dataframe for another dataframe 
Python :: custom django user model 
Python :: django url patterns static 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =