28 lines
464 B
Python
28 lines
464 B
Python
# 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
|
|
"""
|