class Rectangle:
NUMBER_OF_SIDES = 4
def __init__(self, w, h, o):
self.width = w
self.height = h
self.origin = o
def area(self):
return self.width * self.height
def shift(self, dx, dy):
self.origin = (self.origin[0] + dx, self.origin[1] + dy)
r1 = Rectangle(10, 20, (0,0))
r2 = Rectangle(20, 10, (-10,5))
print(r1.origin)
r1.shift(5, 12)
print("r1", r1.origin, r1.area())
print("r2", r2.origin, r2.area())
class Rectangle:
class Rectangle:
class Rectangle:
NUMBER_OF_SIDES = 4
[...]
we define a class attribute NUMBER_OF_SIDES. There
is only one NUMBER_OF_SIDES shared by the class object
and all instances of Rectangle, i.e., changing
the value of NUMBER_OF_SIDE:
Rectangle.NUMBER_OF_SIDES = 6
will change it for all Rectangle
instances as well as the Rectangle class object.class Rectangle:
[...]
def area(self):
return self.width * self.height
[...]
we define the method area. When area is invoked,
the first argument, self, is bound (assigned) to a Rectangle
instance, and values for the width and height
attributes from that instance are used to calculate the return
value.class Rectangle:
NUMBER_OF_SIDES = 4
def __init__(self, w, h, o):
self.width = w
self.height = h
self.origin = o
[...]
r1 = Rectangle(10, 20, (0,0))
r2 = Rectangle(20, 10, (-10,5))
[...]
class Rectangle:
NUMBER_OF_SIDES = 4
def __init__(self, w, h, o):
self.width = w # set instance attribute
self.height = h
self.origin = o
def area(self):
return self.width * self.height # use instance attributes
[...]
r1 = Rectangle(10, 20, (0,0)) # create instance
print(r1.width, r1.height) # use instance attribute
print(r1.NUMBER_OF_SIDES) # use class attribute
print(r1.area()) # call method
class Rectangle:
NUMBER_OF_SIDES = 4
def __init__(self, w, h, o):
self.width = w
self.height = h
self.origin = o
def area(self):
return self.width * self.height
r = Rectangle(20, 10, (0,0))
print(r.NUMBER_OF_SIDES) # use class attribute
r.NUMBER_OF_SIDES = 6 # define _instance_ attribute
print(r.NUMBER_OF_SIDES) # uses new instance attribute!
print(r.area()) # call method
r.area = 300 # define instance attribute
print(r.area()) # call 300 as function?!
class Rectangle:
NUMBER_OF_SIDES = 4
def __init__(self, w, h, o):
self.width = w
self.height = h
self.origin = o
def area(self):
return self.width * self.height
def shift(self, dx, dy):
self.origin = (self.origin[0] + dx, self.origin[1] + dy)
r = Rectangle(20, 10, (0,0))
print(r.area()) # Call _bound_ method
print(Rectangle.area(r)) # Call _unbound_ method
>>> class C:
... def m(self, v):
... print(self, v)
...
>>> i = C()
>>> i.m(10)
<__main__.C object at 0x102f4f950> 10
>>> i.m()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: m() takes exactly 2 arguments (1 given)
>>> i.m(i, 10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: m() takes exactly 2 arguments (3 given)
>>> class Rectangle:
... pass
...
>>> r = Rectangle()
>>> print(r)
<__main__.Rectangle object at 0x100599fd0>
>>> class Rectangle2:
... def __str__(self):
... return "I am a Rectangle2 instance"
...
>>> r2 = Rectangle2()
>>> print(r2)
I am a Rectangle2 instance
>>> class Vector:
... def __init__(self, x, y):
... self.x = x
... self.y = y
... def __str__(self):
... return "(%g,%g)" % (self.x, self.y)
... def __add__(self, other):
... "Overload + operator"
... return Vector(self.x + other.x, self.y + other.y)
...
>>> print(Vector(1, 2) + Vector(2, 4))
(3, 6)