When I
started to learn Python a few months ago, I was introduced that Python is an
object-oriented programming language and the essence of OOP are ‘encapsulation,
polymorphism, and inheritance’. These words used to confuse me. But now, they
start to make sense.
During the
first week, when we analyze the statement about an object called Point, we
designed the attribute (‘coord’) for a point and a method to calculate the
distance from the point to the origin. Instead of directly writing separate
functions to set the ‘coord’ for the point and calculate the distance outside
the class, we write these functions (methods) inside the class and use the
property function to control access to ‘coord’. By creating a class for Point
and infesting it with methods as well as attributes, the program is no longer
just processing data but creating objects that have their own attributes and
methods encapsulated in the class. Only output and input matters and the
process are hidden inside. It’s kind of like us, human – inside us, organ’s
function to keep us alive. All the metabolism and actions are controlled by
things inside us – ‘encapsulated’ and other people could hardly see the process
but the input and output
Then
comes to polymorphism and inheritance. As Prof. Heap talked during lecture,
laziness is the first thing learnt in programming. Here laziness didn’t mean too
lazy to give up tasks but avoiding duplicate code. Like the coord argument
passed to Point object in its __init__ method, by using list comprehension [float(x)
for x in coord], coord with different lengths could be put in and the class
will still work. So we can avoid writing different classes for Points with
different number of coordinates. Another thing about laziness is inheritance. A
class could inherit all the methods from its parent class while still leaves
space for programmers to override or modify the methods based on specific needs.
For instance of the ‘IntStack’ we created later, by reusing __init__ method
from the parent class Stack, it avoids many duplicate codes. Even though
inheritance is useful, it’s better to avoid multiple inheritance since it will
become harder to trace which method the class is actually using which will
cause serious problem when debugging.
When I was
testing my code, I found a flaw of python class. Even though we can use
property function to control access to attributes, we can never fully protect them
from being modified outside the class. Unlike Java where we can use ‘Private’
to hide attributes, in python, ‘_’before the name of attributes and property
function are just nice ways of informing programmers not to modify them.
To conclude,
object-oriented programming is, in some sense, creating real stuff and give it
real attributes and ability for some actions. Like human, all the process are
encapsulated inside the body (encapsulation), different inputs may result in
different outputs but not error (polymorphism), and inheritance.
No comments:
Post a Comment