Skip to content

Instantly share code, notes, and snippets.

@Uiuran
Created October 7, 2019 01:31
Show Gist options
  • Save Uiuran/52e1a0a377621522edbc70fbd024e7c9 to your computer and use it in GitHub Desktop.
Save Uiuran/52e1a0a377621522edbc70fbd024e7c9 to your computer and use it in GitHub Desktop.
Builder with super function as calling Parent Class
class Person(object):
def __init__(self):
self.name = None
self.position = None
self.date_of_birth = None
def __str__(self):
return '{} born on {} works as a {}'.format(self.name,self.date_of_birth,self.position)
# @staticmethod
# def new():
# return PersonBuilder()
class PersonBuilder(Person):
def __init__(self):
super( PersonBuilder, self).__init__()
# self.person = Person()
def build(self):
return self
class PersonInfoBuilder(PersonBuilder):
def called(self, name):
self.name = name
return self
class PersonJobBuilder(PersonInfoBuilder):
def works_as_a(self, position):
self.position = position
return self
class PersonBirthDateBuilder(PersonJobBuilder):
def born(self, date_of_birth):
self.date_of_birth = date_of_birth
return self
if __name__ == '__main__':
pb = PersonBirthDateBuilder()
me = pb\
.called('Dmitri')\
.works_as_a('quant')\
.born('1/1/1980')\
.build() # this does NOT work in C#/C++/Java/...
print(me)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment