Quick Recap
Quick Recap
Recap
c = Circle() à c in an instance of Circle
class Circle:
origin = (0, 0) class attribute – Circle.origin
def __init__(self, r): custom initializer à instance method (bound to instance)
self._r = r
_r is an instance attribute (private by convention only)
@staticmethod à can still access using c._r
def create_unit_circle():
return Circle(1) static method – not bound to anything
c2 = Circle.create_unit_circle()
@classmethod
def set_origin(cls, x, y): class method – bound to class
cls.origin = (x, y)
Circle.set_origin(10, 10)
à can also be called using c.set_origin(10, 10)
def double_radius(self):
self._r *= 2 instance method – bound to instance
c.double_radius()
@property
def radius(self):
return self._r
instance property (radius) with getter/setter methods
@radius.setter
def radius(self, value):
self._r = value
©2019 MathByte Academy