Sunday, January 26, 2014

week 3 --Object-Oriented programming

1.things confusing about  Inheritance

Here is something I find confusing.

class Parent(object):

    def altered(self):
           print "PARENT altered()"

class Child(Parent):

    def altered(self):
        print "CHILD, BEFORE PARENT altered()"
        super(Child, self).altered()
        print "CHILD, AFTER PARENT altered()"

dad = Parent()
son = Child()

dad.altered()
son.altered()

As an example in online textbook, the outcome is like this:
PARENT altered()
CHILD, BEFORE PARENT altered()
PARENT altered()
CHILD, AFTER PARENT altered()


It is sad that the online book does not explain how "super" works. So I try to use some stupid ways to figure it out.

It turns out by replacing "super(Child, self)" with "super()" makes not no difference.
In this case, unless there must be something we do not know about "super", since I really can not find out an alternative way to filling the parameters.
I means except "Child, self", there is nothing else you can put into.


2. The magic thing about namespace

At the first time touching Python, I was kind of disappointed. It is different from the programming language I learned. One of most uncomfortable place is that variables change in the function cannot be delivered to outside (without using global or nonlocal). For instance,

a=1

b=2

Def k(a, b):

a=b

b=1

 

And this does not change anything about a and b.

We may use the following method to make it possible. But then the interesting thing comes

a=1                    
b=2
def k():
global a  
global b
a=b
b=1

print (a,b)

 

this is okay. after running, a and b exchange their values, but if we want some complicated things.

def first():
    def second():
        def third():
            def fourth():
                global a
                a=1
            fourth()
            %
        third()
        %
    second()
    %
%
 
 
first()

 

This one performs well. You can place ‘print(a)’ at any place with % and it turns out the value of innermost a is well delivered.

 

But if you change ‘global’ into ‘nonlocal’, then an unbelieve error happens! It says “no binding for nonlocal ‘a’ found ” wherever you put ‘print(a)’. And even you do not put it, it still raises exception.

 

After trying many many combinations, adding some boring track-variables in order to find out what happened, I finally get what I want.

To summary:

 

def first():
    (1)
    def second():
   (2)
           def third():
               (3)
               def fourth():
                     nonlocal a
                     a=1
               fourth()
           third()   
    second()
first()

 

By adding things like ‘a=3’ at either one of (1)(2)(3), the error will disappear.

 

why would global and nonlocal work so differently?

It seems that nonlocal follows a rule that you want to use it, it must exist and

global will simply create a new one.

 

Anyway, it takes me several boring hours to find this out. I do not think the online text explains it well. Hope you find this helpful.