Sterowanie: spacja + shift-spacja || page down + page up || strzałki || esc
Klasa
Atrybuty
Dane
Metody
complex
Dane:
real
imag
Metody:
conjugate
c = 10 + 5j # utwórz obiekt klasy complexc.real # dane: real10.0c.imag # dane: imag5.0c.conjugate() # metody: conjugate(10-5j)class Nazwa:
instrukcje
...class Triangle:
pass# na razie klasa Triangle
# nie zawiera ani danych ani metod
# ale już możemy stworzyć obiekt typu Triangle
t = Triangle()type(t)__main__.Trianglet.a = 3.0 # atrybut a obiektu t
t.b = 4.0 # atrybut b obiektu t
t.c = 5.0 # atrybut c obiektu t
print(t.a, t.b, t.c) # drukuj atrybuty a, b, c3.0 4.0 5.0def drukuj(triangle): # każdy obiekt może być argumentem funkcji
print(triangle.a, triangle.b, triangle.c)
drukuj(t)3.0 4.0 5.0__init__ - prawie jak konstruktor, ale wywoływana po utworzeniu instancjiclass Triangle:
"""Dokumentacja jak w przypadku funkcji"""
def __init__(self, a, b, c):
"""Prawie jak konstruktor"""
self.a = a # zmiennej obiekt.a
self.b = b # przypisz wartość a
self.c = c # itdt = Triangle(3, 4, 5) # wywołuje funkcję __init__(3, 4, 5)print(t.a, t.b, t.c)3 4 5class Triangle:
"""Dokumentacja jak w przypadku funkcji"""
def __init__(this, a, b, c):
"""Prawie jak konstruktor"""
this.a = a
this.b = b
this.c = c# wywołuje funkcję __init__(3, 4, 5)
t = Triangle(3, 4, 5)print(t.a, t.b, t.c)3 4 5help(t)Help on Triangle in module __main__ object:
class Triangle(builtins.object)
| Dokumentacja jak w przypadku funkcji
|
| Methods defined here:
|
| __init__(this, a, b, c)
| Prawie jak konstruktor
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)__init__ jest jedną z metod specjalnych klasy__nazwa__)__repr__ - "oficjalna" reprezentacja obiektu, powinna być jednoznaczna; wywołana przez repr(obiekt) lub w interpreterze po wpisaniu nazwy zmiennej__str__ - "nieformalna" reprezentacja obiektu, powinna być czytelna; wywołana przez str(obiekt) lub printclass Triangle:
# pomijamy dokumentację: oszczędność slajdu
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def __str__(self): # zwraca string
return "Trójkąt o bokach: {}, {}, {}"\
.format(self.a, self.b, self.c)
def __repr__(self): # zwraca string
return "Triangle({}, {}, {})"\
.format(self.a, self.b, self.c)t = Triangle(3, 4, 5)
print(t) # wywołuje __str__Trójkąt o bokach: 3, 4, 5t # (w interpreterze) wywołuje __repr__Triangle(3, 4, 5)class Triangle:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def obwod(self):
# do zmiennych obiektu odwołujemy się przez self.zmienna
return self.a + self.b + self.ct = Triangle(3, 4, 5)t.obwod()12from math import sqrt
class Triangle:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def obwod(self):
return self.a + self.b + self.c
def pole(self):
# do metod odwołujemy się przez self.metoda
p = self.obwod()/2
return sqrt(p*(p - self.a)*(p - self.b)*(p - self.c))t = Triangle(3, 4, 5)
print("Obwód =", t.obwod())
print("Pole =", t.pole())Obwód = 12
Pole = 6.0from math import sqrt
class Wektor:
"""Trójwymiarowy wektor"""
def __init__(self, x=0.0, y=0.0, z=0.0):
self.x = x
self.y = y
self.z = z
def norm(self):
"""Długość wektora"""
return sqrt(self.x**2 + self.y**2 + self.z**2)w = Wektor(1) # utwórz wektor (1, 0, 0)
w.norm()1.0w.x = 2 # zmień składową x
w.norm()2.0class Wektor:
"""Trójwymiarowy wektor"""
def __init__(self, x=0.0, y=0.0, z=0.0):
self._x = x # zmienne "prywatne"
self._y = y # sygnalizuje się _
self._z = z
def __str__(self):
return "[{}, {}, {}]".format(self._x, self._y, self._z)from wektor import Wektor
w = Wektor(1) # utwórz wektor (1, 0, 0)
print(w)[1, 0.0, 0.0]# dostęp do _zmienna nie jest ograniczony
# jest to tylko wskazówka, żeby nie ruszać
w._x = 2.0
print(w)[2.0, 0.0, 0.0]class Wektor:
"""Trójwymiarowy wektor"""
def __init__(self, x=0.0, y=0.0, z=0.0):
self.__x = x # zmienne zaczynające się __
self.__y = y # zamieniane są na
self.__z = z # _nazwa_klasy__zmienna
def __str__(self):
return "[{}, {}, {}]".format(self.__x, self.__y, self.__z)w = Wektor(1) # [1, 0, 0]
print(w)[1, 0.0, 0.0]w.__x = 2 # nie zmienia skladowej __x
print(w)[1, 0.0, 0.0]w._Wektor__x = 2 # zmienia składową __x
print(w)[2, 0.0, 0.0]class Wektor:
"""Trójwymiarowy wektor"""
def __init__(self, x=0.0, y=0.0, z=0.0):
self.x = x
self.y = y
self.z = z
def __str__(self):
return "[{}, {}, {}]".format(self.x, self.y, self.z)
def __add__(self, w): # operator dodawania
return Wektor(self.x + w.x, self.y + w.y, self.z + w.z)x = Wektor(1, 2, 3)
y = Wektor(2, 4, 6)
print(x + y) # wywołuje x.__add__(y)[3, 6, 9]class Wektor:
"""Trójwymiarowy wektor"""
def __init__(self, x=0.0, y=0.0, z=0.0):
self.x = x
self.y = y
self.z = z
def __str__(self):
return "[{}, {}, {}]".format(self.x, self.y, self.z)
def __mul__(self, w): # operator mnożenia
return self.x*w.x + self.y*w.y + self.z*w.zx = Wektor(1, 2, 3)
y = Wektor(2, 4, 6)
print(x*y) # wywołuje x.__mul__(y)28class Wektor:
"""Trójwymiarowy wektor"""
def __init__(self, x=0.0, y=0.0, z=0.0):
self.x = x
self.y = y
self.z = z
def __str__(self):
return "[{}, {}, {}]".format(self.x, self.y, self.z)
def __mul__(self, w): # operator mnożenia
if type(w) == type(self): # dla wektora - iloczyn skalarny
return self.x*w.x + self.y*w.y + self.z*w.z
else: # dla liczby - mnożenie składowych
return Wektor(w*self.x, w*self.y, w*self.z)x = Wektor(1, 2, 3)
y = Wektor(2, 4, 6)
print(x*y) # wywołuje x.__mul__(y)28print(x*10) # mnoży wektor z przez 10[10, 20, 30]print(10*x) # nie działa...TypeError: unsupported operand type(s) for *: 'int' and 'Wektor'class Wektor:
"""Trójwymiarowy wektor"""
def __init__(self, x=0.0, y=0.0, z=0.0):
self.x = x
self.y = y
self.z = z
def __str__(self):
return "[{}, {}, {}]".format(self.x, self.y, self.z)
def __mul__(self, w): # operator mnożenia
if type(w) == type(self): # dla wektora - iloczyn skalarny
return self.x*w.x + self.y*w.y + self.z*w.z
else: # dla liczby - mnożenie składowych
return Wektor(w*self.x, w*self.y, w*self.z)
def __rmul__(self, w): # mnożenie z prawej
return self.__mul__(w)x = Wektor(1, 2, 3)
print(x*10)[10, 20, 30]print(10*x)[10, 20, 30]