class Point:
x = 0
y = 0
p = Point()
p.x
p.y
p.x = 7
q = Point()
q.x
What about this?
q.x = 10
p.x
class Point:
x = 0
y = 0
Point
Point.__dict__.keys()
Notice how classes are just a namespace!
Point.x
Point.y
p = Point()
p.x
p.__dict__
p.x = 10
p.__dict__
q = Point()
q.x
Point.x = 30
q.x
p.x
q.__dict__
p.__dict__
class Point:
x = 0
y = 0
def move(self,dx,dy):
self.x = self.x + dx
self.y = self.y + dy
Point.x
Point.y
p = Point()
p.move
Point.move
p.move(10,100)
Point.move(p,10,100)
f = p.move
(p.x,p.y)
for i in range(10): f(100,100)
(p.x,p.y)
class Point:
x = 0
y = 0
def move(self,dx,dy):
self.x = self.x + dx
self.y = self.y + dy
def jump(self,x,y):
self.x = x
self.y = y
def __init__(self,x,y):
self.jump(x,y)
def __repr__(self):
return "Point(" + str(self.x) + "," + str(self.y) + ") with id " + hex(id(self))
def __str__(self):
return "x = " + str(self.x) + " y = " + str(self.y)
p = Point(100,100)
p
print(p)
str(p)
Subclass (Inheritance) — an implementation notion
Subtype (Substitution) — a specification notion
class Point:
x = 0
y = 0
def __init__(self,x,y):
self.x = x
self.y = y
def move(self,dx,dy):
s = "Moving from " + str(self.x) + " " + str(self.y) + " to "
self.x = self.x + dx
self.y = self.y + dy
print(s + str(self.x) + " " + str(self.y))
def draw(self):
print("Plot a point at " + str(self.x) + " " + str(self.y))
class ColoredPoint(Point):
color = "blue"
def __init__(self,x,y,color):
Point.__init__(self,x,y)
self.color = color
def draw(self):
print("Set color to: " + str(self.color))
Point.draw(self)
class Point:
x = 0
y = 0
def move(self,dx,dy):
self.x = self.x + dx
self.y = self.y + dy
class Circle:
x = 0
y = 0
radius = 10
def move(self,dx,dy):
self.x = self.x + dx
self.y = self.y + dy
class Clown:
def move(self,dx,dy):
print("Ha! Ha! Fooled you!")
def say_joke():
print("knock knock!");
def do_some_moving(p):
for x in [-2,-1,1,2]:
for y in [-2,-1,1,2]:
p.move(x,y)
p = Point()
q = Clown()
do_some_moving(p)
do_some_moving(q)
p
and q
of the same type?draw
and is an instance of Shape, it is unclear at compile time which draw
to call between the Rectangle
class and the Circle
class.void drawAll(Shape[] shapes) {
for(Shape s: shapes)
s.draw()
}
# Overlaoding a function to take multiple arguments
def add(*args):
# if datatype is int, initialize answer as 0. Use the isinstance function!
if isinstance(args[0], int):
answer = 0
# if datatype is str, initialize answer as ''. Use the isinstance function!
if isinstance(args[0], str):
answer =''
# Traverse through the arguments
for x in args:
# This will do addition if the arguments are int. Or concatenation if the arguments are str
answer = answer + x
print(answer)
# Integer
add(5, 6)
# String
add('Hi ', 'Geeks')
class ColorTextBox(ColorBox,TextPoint):
def draw(self,screen,pos):
ColorBox.draw(self,screen,pos)
r=TextPoint.draw(self,screen,pos)
return r
def __str__(self):
return ColorBox.__str__(self) + " text: " + str(self.text)
class X:
a = 0
def f(self):
return "X"
class Y:
b = 1
def f(self):
return "Y"
class Z:
c = 2
def f(self):
return "Z"
class A(X,Y): pass
class B(Y,Z): pass
class M(A,B,Z): pass
M.__mro__
M().f()
what's happening here?
def g(y): return y + n
g(5)
n = 10
g(5)
n = 100
g(5)
Here is another issue in Python:
n=4
def f():
n = "smash"
print(n)
f()
n
n=4
def f():
global n
n = "smash"
print(n)
f()
n
Here is another workaround:
n = [100]
def f():
n[0] = "smash"
print(n)
f()
n
x = 10
def f():
x = x + 1
print(x)
f()
x = 10
def f():
print("A", x)
y = x
print("B")
x = x + 1
print(x)
f()
x = 10
def f():
global x
x = x + 1
print(x)
f()