Python--lnheritance,Priavte and class-local

来源:互联网 发布:网游推荐知乎 编辑:程序博客网 时间:2024/06/05 20:15

9.5 Inheritance

Execution of a derived class definition proceeds the same as for a base class.

There’s nothing special about instantiation of derived classes: DerivedClassName() creates a new instance of the class. Method references are resolved as follows: the corresponding class attribute is searched, descending down the chain of base classes if necessary, and the method reference is valid if this yields a function object

The method is valid if the search process yields a function object.
  其实有时候我也会被method和function object搞晕,但是目前我的理解为。本质为同一个东西,但是呢,对于class object来说,function obeject为其attribute, 然而对于instance object来说,function object对于其为method。搞懂这些对于编码没有什么明显帮助,但是对于理解python的一些概念还是蛮重要的。

Derived classes may override methods of their base classes. Because methods have no special privileges when calling other methods of the same object, a method of a base class that calls another method defined in the same base class may end up calling a method of a derived class that overrides it.

为了理解上面的话,最好做个试验:

class base_a ():    def put_b(self):        print("A")    def put_a(self):        self.put_b()class a(base_a):    def put_b(self):         print("B")#first testc=base_a()c.put_a()#second testb=a()b.put_a()

结果输出:

[root@localhost ~]# python2.7 test.pyAB

  如果单从结果分析和上面的描述一致,但是如果更往深了思考:methond override也许是namespace中object and instance映射的重新改变呢?
  但是可以确定的是,如果想知道中间发生了什么。NAMSPACE必须深入了解

There is a simple way to call the base class method directly: just call BaseClassName.methodname(self, arguments).

针对这个问题,可以稍微修改上面的代码:

class base_a ():    def put_b(self):        print("A")    def put_a(self):        self.put_b()class a(base_a):    def put_b(self):        print("B")    def put_c(self):        base_a.put_b(self)#first testc=base_a()c.put_a()#second testb=a()b.put_a()b.put_c()

输出结果为:

[root@localhost ~]# python2.7 test.pyABA

NOTE:不得不说,一个比较重要的部分就是new code和old code style的区别,这里一个建议的方式使用super()。

9.6. Private Variables and Class-local References

class basea ():    def __init__(self):        self.__private=1        self.public=2    def put_pub(self):        print(slef.public)    def put_pri(self):        print(self.__private)#first testc=basea()c.put_pri()print c.__privateprint c._basea__

  看运行结果:

[root@localhost ~]# python2.7 test.py 1Traceback (most recent call last):  File "test.py", line 17, in <module>    print c.__privateAttributeError: basea instance has no attribute '__private'

  我想很多人和我一样,都不太理解或者深刻理解。class内部可以访问,外部不能访问的真正意义。从这样的设计本身的应用角度思考也许更加深刻,这也能帮助我们更好的运用语言特性本身。大部分的设计都不是鸡肋

#first testc=basea()c.put_pri()print c._basea__private#second testc._basea_private=2print c._basea__private

结果:

[root@localhost ~]# python2.7 test.py 111

无法改变私有的值,但是也没有报错!

官网例子的深入理解:

理解官方的例子用在我们的代码上:
1.

class basea ():    def __init__(self):        self.__private=1        self.public=2    def put_pub(self):        print(slef.public)    def put_pri(self):        print(self.__private)    def put_parent(self):        self.put_pri()class a(basea):    def put_pri(self):        print(" private")#third testb=a()b.put_parent()

结果:

[root@localhost ~]# python2.7 test.py  private

也就是说put_parent被重写了。但是现在我不想被重写,我调用基类的method,我希望还是使用基类的method来实现。修改代码如下:
2.

class basea ():    def __init__(self):        self.__private=1        self.public=2    def put_pub(self):        print(slef.public)    def put_pri(self):        print(self.__private)    def put_parent(self):        self.__put_pri()    __put_pri=put_priclass a(basea):    def put_pri(self):        print(" private")#third testb=a()b.put_parent()

结果:

[root@localhost ~]# python2.7 test.py 1

这里使用的方式不亚于直接调用两个method直接,但是使用__(private)的方式对子类和外部都隐藏起来。与编程有关的重要概念就是命名空间。我使用C语言时间比较长,与变量的作用域有点像,却又不是一回事。下次将学习深入下命名空间。

原创粉丝点击