In [195]:
class A(object):
def __init__(self, **kwargs):
print('A.__init__ with {}'.format(kwargs))
super(A, self).__init__()
class B(A):
def __init__(self, **kwargs):
print('B.__init__ with {}'.format(kwargs))
super(B, self).__init__(**kwargs)
class C(A):
def __init__(self, **kwargs):
print('C.__init__ with {}'.format(kwargs))
super(C, self).__init__(**kwargs)
class D(B, C):
def __init__(self):
print('D.__init__')
super(D, self).__init__(a=1, b=2, x=3)
In [196]:
print(D.mro())
D()
Out[196]:
In [31]:
class A(object):
def __init__(self, a):
self.a = a
class B(A):
def __init__(self, b, **kw):
self.b = b
super(B, self).__init__(**kw)
class C(A):
def __init__(self, c, **kw):
self.c = c
super(C, self).__init__(**kw)
class D(B, C):
def __init__(self, a, b, c):
super(D, self).__init__(a=a, b=b, c=c)
In [32]:
obj = D(1,2,3)
In [197]:
obj.a, obj.b, obj.c
Out[197]:
In [68]:
class First(object):
def __init__(self):
print "first"
class Second(First):
def __init__(self):
print "second before super"
super(Second, self).__init__()
print "second after super"
class Third(First):
def __init__(self):
print "third before super"
super(Third, self).__init__()
print "third after super"
class Fourth(Second, Third):
def __init__(self):
print "fourth before super"
super(Fourth, self).__init__()
print "that's it"
In [69]:
Fourth()
Out[69]:
In [75]:
class First(object):
def __init__(self):
print "first"
class Second(First):
def __init__(self):
print "second before super"
super(Second, self).__init__(a=2)
print "second after super"
class Third(First):
def __init__(self, a):
print "third before super"
super(Third, self).__init__()
print "third after super"
class Fourth(Second, Third):
def __init__(self):
print "fourth before super"
super(Fourth, self).__init__()
print "that's it"
In [76]:
Fourth()
Out[76]:
In [77]:
Second()
In [81]:
class A():
def suma(self, a, b):
return a + b
class AzMnozeniem(A):
def mnozenie(self, a, b):
return a * b
In [82]:
k = AzMnozeniem()
In [83]:
k.mnozenie(3, 4)
Out[83]:
In [84]:
k.suma(3, 4)
Out[84]:
In [88]:
class A(object):
def __init__(self, a):
self.a = a
def __add__(self, other):
self.a += other.a
return self
In [90]:
(A(4) + A(5)).a
Out[90]:
object.__lt__(self, other)
object.__le__(self, other)
object.__eq__(self, other)
object.__ne__(self, other)
object.__gt__(self, other)
object.__ge__(self, other)
object.__add__(self, other)
object.__sub__(self, other)
object.__mul__(self, other)
object.__floordiv__(self, other)
object.__mod__(self, other)
object.__divmod__(self, other)
object.__pow__(self, other[, modulo])
object.__lshift__(self, other)
object.__rshift__(self, other)
object.__and__(self, other)
object.__xor__(self, other)
object.__or__(self, other)
In [93]:
import abc
class Person():
__metaclass__ = abc.ABCMeta
def __init__(self, name):
self.name = name
@abc.abstractmethod
def say_hello(self):
pass
class Programmer(Person):
def __init__(self, name, language):
Person.__init__(self, name)
self.language = language
def say_hello(self):
print('Hello! I\'m %s and I write in %s.' % (self.name, self.language))
In [98]:
p = Person(name="Duck")
p
In [97]:
p = Programmer(name="Duck", language="Duck++")
p
Out[97]:
In [99]:
p.say_hello()
In [105]:
class A(object):
b = "0.001"
def __init__(self, a):
self.a = a
In [106]:
A.b
Out[106]:
In [107]:
A.a
In [108]:
A(234).a
Out[108]:
In [111]:
class A():
j = 0
for i in range(10):
j += i
In [112]:
A.j
Out[112]:
In [146]:
import math
import random
class Pole(object):
def __init__(self, r=2.):
self.r = r
def oblicz(self):
return math.pi * (self.r**2)
In [147]:
n = 1000
def get_mean_cls(n=n):
return sum([Pole(random.random()).oblicz() for i in range(n)])/float(n)
def get_mean(n=n):
return sum([math.pi * (random.random()**2) for r in range(n)])/float(n)
In [152]:
%timeit get_mean_cls()
In [153]:
%timeit get_mean()
In [161]:
class Kaczka(object):
def __init__(self, dl_skrzydla):
self.dl_skrzydla = dl_skrzydla
def plyn(self):
print "Chlup chlup"
In [162]:
k = Kaczka(124)
k.plyn()
k.dl_skrzydla
Out[162]:
In [165]:
class Kaczuszka(Kaczka):
def __init__(self, dl_skrzydla):
self._dl_skrzydla = dl_skrzydla
@property
def dl_skrzydla(self):
return self._dl_skrzydla / 2.
@dl_skrzydla.setter
def dl_skrzydla(self, value):
self._dl_skrzydla = value
In [184]:
k = Kaczuszka(124)
k.plyn()
k.dl_skrzydla
Out[184]:
In [188]:
k.dl_skrzydla = 100
k.dl_skrzydla
Out[188]:
In [189]:
k.dl_skrzydla += 50
In [190]:
k.dl_skrzydla
Out[190]:
1 . Stworz klasę koło w którą inicjalizuje się promieniem.
Klasa ta powinna mieć metody obliczające:
2 . Napisz klasę opona która również inicjalizujesz promieniem.
Obwód opony jest 1.25 razy większy od obowodu koła.
3 . Do liczenia pola w klasie koło wykorzystaj metodę licząca obwód. (Przepisy to regulują nic nie możesz z tym zrobić, wiem to nieoptymalne, ale mus to mus)
4 . Co się stało z klasa opony? Czy jej pole liczone jest poprawnie? Zabezpiecz kod przed tym!
5 . Nowy rząd dużo ostatnio mąci... mówi, że nie wolno nam zapisywać atrybutu "promień" w kole, musimy zapisywać średnice, spraw aby kod klasy opona nadal działał
Postaraj się, żeby kod był ładny, napisz docstringi, wersje ...