Tuesday, October 1, 2013

Inheritance (Python)

If you master the skill of playing around with namespace, then inheritance in Python would make a lot more sense. Python's abstract data structure is different than Java, so if you are reading this and you had Java background, forget about the relationship of a class with its methods, and the inheritance structure of the parent class or superclass. It's easier to understand if you accept this new idea of inheritance in Python than trying to match the similarities with Java. By the way, when I say you, I meant me; is the way I take note. So when I am rereading this, I would nod to myself, and I feel like 'this guy knows all my weakness, we must be similar'.

Namespace 

To understand namespace, first you have to know that everything in Python is treated as an object. Class is an object, function is an object, attribute is an object, even module is an object, everything is treated as an object. That is why you can access pretty much everything with just dot-something, for example: math.sqrt, math.sqrt(), animal_file.Dog.bite.blood, etc. Because of that, there is no private  in Python, but is convention to state with with an underscore, _object, to indicate that changing this object might crash the code. If you afraid that someone might accidentally replace the object by having an object with the same name, then you use TWO underscores, __object, is the same as writing  Class._object. Then no matter how bad the someone is import your module, no other object can replace your object.

Now, that we clarify that everything is treated as an object in Python, lets see what is namespace. Namespace is a mapping from names to objects. Is a space where it contains the names of the objects. Imagine a grocery store, each item contains a unique bar code, now collect all the bar codes of all the items in that grocery store. The namespace is the list of all the bar codes of that grocery store. This is why Python doesn't  need you to initialize the variable. 

In a Python file, there are several namespaces on different hierarchy. On the top of the hierarchy is the module namespace, then there is class namespace and within a class there is a namespace and furthermore, each function has there own namespace. When you call __main__, there is a namespace for that module and on top of all of that there is the __builtin__ module's namespace which contain the builtin name. Namespace is a space that contain the names of the objects. A scope is a region of the code where a namespace is directly accessible. For example:


_______________________________________________________________________
class fooEx():
foo = "hello"

def foo_function():
          foo = "hey"
          return foo

>>>foo_function()
>>>print(foo)
#output: hello
_________________________________________________________________________________


The foo in the class, has a scope of the class namespace and the foo in the function has a scope of the function namescope. So that means the foo in the class is different than the foo in the function although they have the same name. However you can specify the foo in the function to be in the foo in the class, with the key word, nonlocal.

_______________________________________________________________________

class fooEx():
foo = "hello"

def foo_function():
         nonlocal foo
         foo = "hey"
         return foo

>>>foo_function()
#output: hey
_________________________________________________________________________________

If you want the variable accessible throughout the whole module, use global instead of nonlocal.

Inheritance

Let's say you have to write two classes: Iron_Man and Inception; and your classes need to included the elements that make those movies such a big hit. You will notice that most of the elements of those movies are similar, and you are tired of rewriting those elements again and again. The solution is to write a parent class for those two classes. In Python is really easy to state the class is the child of a class; you just indicate it inside the parenthesis when you are declaring the class. For example:
_________________________________________________________________________________
class Iron_Man(Action):
            pass
class Inception(Action):
            pass
class Action():
           soundeffect = ["boom!", "Whoaa!"]
           def explosion():
                         pass
_________________________________________________________________________________

Every object in the Action class can be access from the child class, so Iron_Man and Inception, both contain the object soudeffect and the function explosion. If you want to add another parent to Inception, add a coma and the name of the parent class after adding the first parent.

_________________________________________________________________________________
class Iron_Man(Action):
            pass

class Inception(Action, Mind_bending):
            pass

class Action():
           soundeffect = ["boom!", "Whoaa!"]
           def explosion():
                         pass

class Mind_bending():
                         pass
_________________________________________________________________________________

Inception class now has the access to Action and Mind_bending class. 

No comments:

Post a Comment