📚 The CoCalc Library - books, templates and other resources
License: OTHER
"""This module contains a code example related to12Think Python, 2nd Edition3by Allen Downey4http://thinkpython2.com56Copyright 2015 Allen Downey78License: http://creativecommons.org/licenses/by/4.0/9"""1011from __future__ import print_function, division121314class Point:15"""Represents a point in 2-D space.1617attributes: x, y18"""19def __init__(self, x=0, y=0):20self.x = x21self.y = y2223def __str__(self):24return '(%g, %g)' % (self.x, self.y)2526def __add__(self, other):27"""Adds a Point or tuple."""28if isinstance(other, Point):29return self.add_point(other)30elif isinstance(other, tuple):31return self.add_tuple(other)32else:33msg = "Point doesn't know how to add type " + type(other)34raise TypeError(msg)3536def add_point(self, other):37"""Adds a point."""38return Point(self.x + other.x, self.y + other.y)3940def add_tuple(self, other):41"""Adds a tuple."""42return Point(self.x + other[0], self.y + other[1])43444546def main():47p1 = Point(1, 2)48p2 = Point(3, 4)49print(p1)50print(p2)51print(p1 + p2)52print(p1 + (3, 4))5354if __name__ == '__main__':55main()56575859