ch06
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
# oop/class.methods.factory.py
|
||||
class Point:
|
||||
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
@classmethod
|
||||
def from_tuple(cls, coords): # cls is Point
|
||||
return cls(*coords)
|
||||
|
||||
@classmethod
|
||||
def from_point(cls, point): # cls is Point
|
||||
return cls(point.x, point.y)
|
||||
|
||||
|
||||
p = Point.from_tuple((3, 7))
|
||||
print(p.x, p.y) # 3 7
|
||||
q = Point.from_point(p)
|
||||
print(q.x, q.y) # 3 7
|
||||
|
||||
|
||||
"""
|
||||
$ python class.methods.factory.py
|
||||
3 7
|
||||
3 7
|
||||
"""
|
||||
Reference in New Issue
Block a user