mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
python3/en cleanup for single inheritance merge
This commit is contained in:
parent
9b6d84309c
commit
55efb934b8
@ -785,7 +785,7 @@ class Superhero(Human):
|
|||||||
self.superpowers = superpowers
|
self.superpowers = superpowers
|
||||||
|
|
||||||
# The "super" function lets you access the parent class's methods
|
# The "super" function lets you access the parent class's methods
|
||||||
# that are overwritten by the child, in this case, the __init__ method.
|
# that are overridden by the child, in this case, the __init__ method.
|
||||||
# This calls the parent class constructor:
|
# This calls the parent class constructor:
|
||||||
super().__init__(name)
|
super().__init__(name)
|
||||||
|
|
||||||
@ -814,7 +814,7 @@ if __name__ == '__main__':
|
|||||||
# => <class 'human.Human'>, <class 'object'>)
|
# => <class 'human.Human'>, <class 'object'>)
|
||||||
|
|
||||||
# Calls parent method but uses its own class attribute
|
# Calls parent method but uses its own class attribute
|
||||||
print(sup.get_species()) # => Superhero
|
print(sup.get_species()) # => Superhuman
|
||||||
|
|
||||||
# Calls overloaded method
|
# Calls overloaded method
|
||||||
print(sup.sing()) # => Dun, dun, DUN!
|
print(sup.sing()) # => Dun, dun, DUN!
|
||||||
@ -838,6 +838,7 @@ if __name__ == '__main__':
|
|||||||
####################################################
|
####################################################
|
||||||
|
|
||||||
# Another class definition
|
# Another class definition
|
||||||
|
# bat.py
|
||||||
class Bat:
|
class Bat:
|
||||||
|
|
||||||
species = 'Baty'
|
species = 'Baty'
|
||||||
@ -859,16 +860,13 @@ if __name__ == '__main__':
|
|||||||
print(b.say('hello'))
|
print(b.say('hello'))
|
||||||
print(b.fly)
|
print(b.fly)
|
||||||
|
|
||||||
# To take advantage of modularization by file you could place the classes above in their own files,
|
|
||||||
# say, superhero.py and bat.py
|
|
||||||
|
|
||||||
# To import functions from other files use the following format
|
# And yet another class definition that inherits from Superhero and Bat
|
||||||
# from "filename-without-extension" import "function-or-class"
|
|
||||||
# superhero.py
|
# superhero.py
|
||||||
from superhero import Superhero
|
from superhero import Superhero
|
||||||
from bat import Bat
|
from bat import Bat
|
||||||
|
|
||||||
# Batman inherits from both Superhero and Bat
|
# Define Batman as a child that inherits from both Superhero and Bat
|
||||||
class Batman(Superhero, Bat):
|
class Batman(Superhero, Bat):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
@ -892,14 +890,6 @@ class Batman(Superhero, Bat):
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sup = Batman()
|
sup = Batman()
|
||||||
|
|
||||||
# Instance type checks
|
|
||||||
if isinstance(sup, Superhero):
|
|
||||||
print('I am a superhero)
|
|
||||||
if isinstance(sup, Bat):
|
|
||||||
print('I am bat')
|
|
||||||
if type(sup) is Batman:
|
|
||||||
print('I am Batman')
|
|
||||||
|
|
||||||
# Get the Method Resolution search Order used by both getattr() and super().
|
# Get the Method Resolution search Order used by both getattr() and super().
|
||||||
# This attribute is dynamic and can be updated
|
# This attribute is dynamic and can be updated
|
||||||
print(Batman.__mro__) # => (<class '__main__.Batman'>,
|
print(Batman.__mro__) # => (<class '__main__.Batman'>,
|
||||||
|
Loading…
Reference in New Issue
Block a user