GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ThinkPython2/code/Point2_soln.py at master · AllenDowney/ThinkPython2

Expand file tree

/

Point2_soln.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
57 lines (41 loc) · 1.22 KB
/

Point2_soln.py

Copy path

File metadata and controls

57 lines (41 loc) · 1.22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""This module contains a code example related to
Think Python, 2nd Edition
by Allen Downey
https://greenteapress.com/wp/think-python-2e/
Copyright 2015 Allen Downey
License: http://creativecommons.org/licenses/by/4.0/
"""
from __future__ import print_function, division
class Point:
"""Represents a point in 2-D space.
attributes: x, y
"""
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __str__(self):
return '(%g, %g)' % (self.x, self.y)
def __add__(self, other):
"""Adds a Point or tuple."""
if isinstance(other, Point):
return self.add_point(other)
elif isinstance(other, tuple):
return self.add_tuple(other)
else:
msg = "Point doesn't know how to add type " + type(other)
raise TypeError(msg)
def add_point(self, other):
"""Adds a point."""
return Point(self.x + other.x, self.y + other.y)
def add_tuple(self, other):
"""Adds a tuple."""
return Point(self.x + other[0], self.y + other[1])
def main():
p1 = Point(1, 2)
p2 = Point(3, 4)
print(p1)
print(p2)
print(p1 + p2)
print(p1 + (3, 4))
if __name__ == '__main__':
main()
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.