Pokemon('Pikachu', 100, 50)
, and then a Pikachu instance is just created.
In [2]:
# Define a class named Pokemon
class Pokemon():
def __init__(self, name, attack, defence):
self.name = name
self.attack = attack
self.defence = defence
print('Hello world')
def poko_name(self):
return self.name
def poko_state(self):
return self.attack, self.defence
def __str__(self):
return 'My name is %s, and my attack is %d, defence is %d.' %(self.name, self.attack, self.defence)
In [3]:
Pikachu = Pokemon('Pikachu', 100, 50)
In [7]:
Pikachu.poko_name()
Out[7]:
In [6]:
Pikachu.poko_state()
Out[6]:
In [23]:
Pikachu.name
Out[23]:
__init__
?__init__
method is a special Python function that is called when an instance of a class is first created.Pikachu = Pokemon('Pikachu', 100, 50)
, the __init__
method is called with values Pikachu, "Pikachu", 100, and 50 for the variables self, name, attack and defence, respectively.self
?self
is the instance. In this case, self
represents the Pikachu instance itself.__init__
method (self, name, attack, defence), why we only need to fill in 3 (e.g. Pikachu = Pokemon('Pikachu', 100, 50)
) ?
In [45]:
Pikachu = Pokemon(Pikachu, 'Pikachu', 100, 50)
In [8]:
### To make it clear, here's an example.
### Standard way of calling `poko_name`:
Pikachu.poko_name()
### In the case, `self` represents the Pikachu instance itself, and is automatically passed into the function by Python.
Out[8]:
In [9]:
### Inconventional way:
Pokemon.poko_name(Pikachu)
Out[9]:
In [10]:
### if we didn't do that
Pokemon.poko_name()
### then there is a variable missed.
In [11]:
### what __str__ does.
print(Pikachu)
In [12]:
class Electric(Pokemon):
def __init__(self, name, attack, defence):
Pokemon.__init__(self, name, attack, defence)
In [13]:
Raichu = Electric('Raichu', 200, 100)
print(Raichu)
In [14]:
class Electric(Pokemon):
def __init__(self, name, attack, defence):
Pokemon.__init__(self, name, attack, defence)
def __str__(self):
return 'My name is %s, and my attack is %d, defence is %d. I am electric type.' %(self.name, self.attack, self.defence)
In [15]:
Raichu = Electric('Raichu', 200, 100)
Raichu.poko_state()
Out[15]:
In [16]:
### __str__ method from Pokemon is replaced in Electric.
print(Raichu)
class Electric(Pokemon)
means Electric class inherits from Pokemon class. (Electric is a subclass of Pokemon.)object
.class Pokemon()
is totally same as class Pokemon(object)
.
In [17]:
### check instance
isinstance(Raichu, Electric)
Out[17]:
In [18]:
isinstance(Raichu, Pokemon)
Out[18]:
In [19]:
isinstance(Pikachu, Electric)
Out[19]:
In [20]:
isinstance(Pikachu, Pokemon)
Out[20]:
In [21]:
# An example for multiple inheritance:
class Base():
def __init__(self, value):
self.value = value
class Times(Base):
def __init__(self, value):
Base.__init__(self, value)
self.value *= 10
class Plus(Base):
def __init__(self, value):
Base.__init__(self, value)
self.value += 5
class MultInher(Times, Plus):
def __init__(self, value):
Times.__init__(self, value)
Plus.__init__(self, value)
In [22]:
result = MultInher(8)
In [24]:
"""
"""
Out[24]:
In [26]:
# Let's check.
result = MultInher(8)
result.value
Out[26]:
__init__
method in the base class is executed twice. However, most of time, we want it to execute for only one time.
In [27]:
# Define a simple class named A
class A():
def __init__(self, a):
print('enter A')
self.a = a
print(self.a)
print('leave A')
def method(self):
print()
print('method from A')
print(self.a)
In [30]:
# Try
instance = A(3)
instance.method()
In [34]:
# Define a class named B which is inherited from A.
class A():
def __init__(self, a):
print('enter A')
self.a = a
print(self.a)
print('leave A')
def method(self):
print()
print('method from A')
print(self.a)
class B(A):
def __init__(self, b):
print('enter B')
super(B, self).__init__('I give from B')
self.a = 'self.a is changed from B'
print(self.a)
print('leave B')
In [35]:
instance = B(1)
instance.method()
In [38]:
# Another example using super()
class B(A):
def __init__(self, b):
print('enter B')
super().__init__('I give from B')
self.a = 'self.a is changed from B'
print(self.a)
print('leave B')
In [39]:
instance = B(1)
instance.method()
In [40]:
# Use inheritance method I mentioned at beginning.
class B(A):
def __init__(self, b):
print('enter B')
A.__init__(self, 'I give from B')
self.a = 'self.a is changed from B'
print(self.a)
print('leave B')
In [41]:
instance = B(1)
instance.method()
super(B, self)
is totally same as super()
.super()
and A.__init__(self)
In [49]:
class A():
def __init__(self, a):
print('enter A')
super().__init__()
self.a = a
print(self.a)
print('leave A')
def method(self):
print()
print('method from A')
print(self.a)
class B(A):
def __init__(self, b1):
print('enter B')
super().__init__('I give from B')
self.a = 'self.a is changed from B'
print(self.a)
print('leave B')
class C(A):
def __init__(self, c1):
print('enter C')
super().__init__('I give from C')
print('leave C')
def method(self):
print()
print('method from C')
print(self.a)
class D(B, C):
def __init__(self, d):
print('enter D')
self.a = d
print('initial value of self.a:', self.a)
super().__init__('I give from D')
print(self.a)
print('leave D')
In [54]:
D.mro()
Out[54]:
In [55]:
instance = D(3)
instance.method()
In [60]:
class D(C, B):
def __init__(self, d):
print('enter D')
self.a = d
print('initial value of self.a:', self.a)
super().__init__('I give from D')
print(self.a)
print('leave D')
In [62]:
D.mro()
Out[62]:
In [63]:
class D(A, B):
def __init__(self, d):
print('enter D')
self.a = d
print('initial value of self.a:', self.a)
super().__init__('I give from D')
print(self.a)
print('leave D')
In [64]:
class A():
def __init__(self, a):
print('enter A')
super().__init__()
self.a = a
print(self.a)
print('leave A')
def method(self):
print()
print('method from A')
print(self.a)
class B(A):
def __init__(self, b1):
print('enter B')
super().__init__('I give from B')
self.a = 'self.a is changed from B'
print(self.a)
print('leave B')
def method(self):
print()
print('method from B')
print(self.a)
class C(A):
def __init__(self, c1):
print('enter C')
super().__init__('I give from C')
print('leave C')
def method(self):
print()
print('method from C')
print(self.a)
class D(B, C):
def __init__(self, d):
print('enter D')
self.a = d
print('initial value of self.a:', self.a)
super().__init__('I give from D')
print(self.a)
print('leave D')
def method(self):
print()
print('method from D')
print(self.a)
In [66]:
intsance = D(3)
D(3).method()
In [68]:
class A():
def __init__(self, a):
print('enter A')
print('value of self.a:', self.a)
super().__init__('I give from A')
print(self.a)
print('leave A')
def method(self):
print()
print('method from A')
print(self.a)
class B():
def __init__(self, b1):
print('enter B')
print('value of self.a:', self.a)
super().__init__()
self.a = 'self.a is changed from B'
print(self.a)
print('leave B')
def method(self):
print()
print('method from B')
print(self.a)
class C(A):
def __init__(self, c1):
print('enter C')
print('value of self.a:', self.a)
super().__init__('I give from C')
print('leave C')
def method(self):
print()
print('method from C')
print(self.a)
class D(B):
def __init__(self, d):
print('enter D')
self.a = d
print('value of self.a:', self.a)
super().__init__('I give from D')
print(self.a)
print('leave D')
def method(self):
print()
print('method from D')
print(self.a)
class E(C, D):
def __init__(self, e):
print('enter E')
self.a = e
print('value of self.a:', self.a)
super().__init__('I give from E')
print(self.a)
print('leave E')
def method(self):
print()
print('method from E')
print(self.a)
In [70]:
e.__class__.__mro__
Out[70]:
In [69]:
e = E(3)
e.method()
In [71]:
class F(E, C):
def __init__(self, f):
print('enter F')
In [72]:
F(3).__class__.__mro__
Out[72]:
In [73]:
class F(E, C):
def __init__(self, f):
print('enter F')
super(F, self).__init__('I give from F')
In [74]:
F(3).__class__.__mro__
Out[74]:
In [75]:
class F(E, C, B):
def __init__(self, f):
print('enter F')
super(F, self).__init__('I give from F')
In [76]:
F.mro()
Out[76]:
In [77]:
class Base(object):
def __init__(self, value):
self.value = value
class TimesFive(Base):
def __init__(self, value):
super(TimesFive, self).__init__(value)
self.value *= 5
class PlusTwo(Base):
def __init__(self, value):
super(PlusTwo, self).__init__(value)
self.value += 2
class TestOne(TimesFive, PlusTwo):
def __init__(self, value):
super(TestOne, self).__init__(value)
In [80]:
test = TestOne(4)
In [81]:
# test.value???
"""
"""
Out[81]:
In [82]:
test.value
Out[82]: