python_classes_1.html


001| """Python Classes (aka 'data types')."""
002| 
003| class point:
004|     """A Python class to implement a simple X-Y point."""
005| 
006|     def __init__ (self, x:float=0.0, y:float=0.0):
007|         """Initialize new point. Default: [0.0, 0.0]"""
008|         self.x = float(x)
009|         self.y = float(y)
010| 
011|     def __str__ (self):
012|         """Return a print string."""
013|         return f'[{self.x:+.3f}, {self.y:+.3f}]'
014| 
015|     def __repr__ (self):
016|         """Return a debug string."""
017|         return f'<{type(self).__name__} @{id(self):012x}>'
018| 
019|     def reset (self):
020|         """Reset the point to zero."""
021|         self.x = 0.0
022|         self.y = 0.0
023| 
024| 
025| 
026| def main ():
027|     """Exercise the point class and return some points."""
028| 
029|     # Create a bunch of new points...
030|     pt0 = point()
031|     pt1 = point(-3.14, 2.7)
032|     pt2 = point(y=1)
033| 
034|     # Print the points...
035|     print(f'Point0: {pt0} {pt0!r}')
036|     print(f'Point1: {pt1} {pt1!r}')
037|     print(f'Point2: {pt2} {pt2!r}')
038|     print()
039| 
040|     pt0.x = 1.2
041|     pt0.y = 2.4
042|     print(f'Alter pt0: {pt0} {pt0!r}')
043| 
044|     pt0.reset()
045|     print(f'Reset pt0: {pt0} {pt0!r}')
046|     print()
047| 
048|     return (pt0,pt1,pt2)
049| 
050| if __name__ == "__main__":
051|     print()
052|     pts = main()
053|     print()
054| """eof"""
055|