In this week's post I am going to talk about Inheritance and Recursion in Python.
Inheritance and Recursion are two very important process in computer programming. I was also confused when I first learn it. The example function tree_burst(), which I gave in Recursion part, is the one that I found most trouble with. Our prof taught it in class, but it's not very clear. I traced back every step of the recursion, wrote it out in my scrap paper and drew the turtle lines, but I am still confused with turtle_list.append(turtle.clone()).
Turtle module open a new world for me to realize that we can also use python to draw graphs, and this turns the scalar quantities to vector quantities. Furthermore, in this way, I guess we can also create any finite dimensional vector space using computer!
tree_burst() function is not a very good example for beginners to understand recursion, but I still decided to post it out because it is indeed a very impressive function. If anyone feel confused about it, please just skip and move to the next part. (If you have some good idea about the turtle_list.append(), please feel free to communicate! )
A. Inheritance
Literally, inheritance is the practice of passing on the feature to a child upon a parent. Well, in python, inheritance means the subclass(child) get most or all its method(feature) from its superclass (parent).
There are three ways of inheritance:
1. Implicitly Inheritance
which means some or all the features that the child gets from his father didn't show off (we don't necessarily write it out in our subclass)
class Parent(object):
def a(self):
print "PARENT implicit()"
class Child(Parent):
pass
*Here the method "a" is a implicit inheritance of the Child.
2. Override Explicitly
A child is not as same as their parent, same in the Python, sometimes a subclass behaves differently. We are going to override the function in the child, replacing the functionality inherited from its superclass.
class Parent(object):
def override(self):
print "PARENT override()"
class Child(Parent):
def override(self):
print "CHILD override()"
3. Alter Before or After
A child can act completely different without their parent, but once their parent show up, they will behave like an angel.
In python, we can add a parent version of the function runs among the child versions.
class Parent(object):
def altered(self):
print "PARENT altered()"
class Child(Parent):
def altered(self):
print "CHILD, BEFORE PARENT altered()"
Parent.altered(self)
print "CHILD, AFTER PARENT altered()"
*These three inheritance we can use them separately or together
*Child(Parent): the Child class is a subclass of Parent class
*Parent(object), because every class is a subclass of the object class
B. Recursion
That's all for this week, thank you for reading! Next week I am going to talk about unittest, see you next week! :)
B. Recursion
Recursion means "defining something in terms of itself", e.g. A cat is a kind of animal whose mother is a cat. In python, recursion means a function can call themselves to solve smaller subproblems.
Note a finite recursive a function must have a base case, in order to indicate the function where to stop, otherwise the function will run forever and it will become an infinite recursive function.
def tree_burst(level, base, turtle):
"""Draw a symmetrical ternary tree of height level with initial edges
of length base.
"""
if not level: # Pythonese for level == 0
pass
else:
turtle_list = []
for h in range(3):
turtle_list.append(turtle.clone())
turtle_list[h].color(COLORS[h])
turtle_list[h].setheading(120 * h)
turtle_list[h].forward(base)
tree_burst(level - 1, base/2, turtle_list[h])
* Note in this example, the italic format indicates the base case. Specifically, when level == 0, the recursion stops.
That's all for this week, thank you for reading! Next week I am going to talk about unittest, see you next week! :)
in the third part, the children.alter is excessive, since if parent and childen are inheritanced, children will use parent.alter if without defining children.alter.
ReplyDeleteAha, you're right! I made a mistake there
ReplyDelete