Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

class chain methods python

class foo():
    def __init__(self, kind=None):
        self.kind = kind
    def my_print(self):
        print (self.kind)
        return self
    def line(self):
        self.kind = 'line'
        return self
    def bar(self):
        self.kind='bar'
        return self
Comment

class chain methods python

a = foo()
a.line()
a.my_print()
a.bar()
a.my_print()

assert a.kind == 'bar'
Comment

class chain methods python

c = foo().line().my_print().bar().my_print()
assert c.kind == 'bar'
Comment

class chain methods python

class foo():
    def __init__(self, kind=None):
        self.kind = kind
    def my_print(self):
        print (self.kind)
        return self
    @property
    def line(self):
        self.kind = 'line'
        return self
    @property
    def bar(self):
        self.kind='bar'
        return self

a = foo()
a.line
a.my_print()
a.bar
a.my_print()

assert a.kind == 'bar'

b = foo()
b.line.my_print().bar.my_print()
assert b.kind == 'bar'

c = foo().line.my_print().bar.my_print()
assert c.kind == 'bar'
Comment

PREVIOUS NEXT
Code Example
Python :: python 2 print sep end 
Python :: image resolution extracting python 
Python :: concate the dataframe in pandas.. 
Python :: python dictionary delete based on value 
Python :: maximun row and columns in python 
Python :: python filter list with list of booleans 
Python :: how to add some thing in python list 
Python :: install python 3.4 mac terminal 
Python :: relu python 
Python :: {} string python 
Python :: Finding if 2 consecutive numbers in a list have a sum equal to a given number 
Python :: merge two arrays python 
Python :: python compare dates 
Python :: pandas rename columns whitespace with underscore 
Python :: how to round a number up in python 
Python :: all combinations 
Python :: symmetric_difference() Function of sets in python 
Python :: how to check if text is lower in python 
Python :: how to extract values from a dictionary 
Python :: pubg python 
Python :: how to leave a function python 
Python :: lru_cache 
Python :: Concatenating objects in pandas 
Python :: gfg placement course 
Python :: Python script from c++ 
Python :: if condition in python lambda 
Python :: how to plot a pandas dataframe with matplotlib 
Python :: how to split from a specific charecter to the end of the string in python 
Python :: python equivalent of R sample function 
Python :: from html to jupyter notebook 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =