Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gmolveau
GitHub Repository: gmolveau/python_full_course
Path: blob/master/exercices/oop_classmethod.py
305 views
1
class Y:
2
def __init__(self, astring):
3
self.s = astring
4
5
@classmethod # classmethod to build a factory / alternative constructor
6
def fromlist(cls, alist):
7
x = cls('')
8
x.s = ','.join(str(s) for s in alist)
9
return x
10
11
def __repr__(self):
12
return f'y({self.s})'
13
14
# subclassing of Y - .fromlist() still works
15
class K(Y):
16
def __repr__(self):
17
return f'k({self.s.upper()})'
18
19
k1 = K.fromlist(['za','bu'])
20
k1
21