Life Since 1985

Happy Hacking
python中文支持

python新式类的继承方式

david kei posted @ 2010年8月11日 10:27 in Python with tags python , 5380 阅读

最近在读《python学习手册》,发现python新式类的继承方式很有意思,而且据说在python3.0里面所有的类都将变成新式类,在此让同是跟我一样在Python2.x入门的人尝个鲜。

在python的普通中类中,继承的模式是深度优先的,也就是说对于一个对象的搜索遵循从左至右、从下到上的原则,而这从下到上的原则又优于从左至右。发觉语言上的描述十分晦涩,举个例子吧。

# -*- coding:utf-8 -*-
class A():
    """
    作为所有类的基类
    """
    value = 1
    
    def __init__(self, ):
        """
        """
        
class B(A):
    """
    D的基类,继承自A
    """
    
    def __init__(self, ):
        """
        """

class C(A):
    """
    D的基类,继承自A
    并且将基类中的变量值修改
    """
    value = 3
    
    def __init__(self, ):
        """
        """
        
class D(B, C):
    """
    """
    
    def __init__(self, ):
        """
        """
        
x = D()
print x.value

对于以上的代码输出的结果将是1。

因为对于经典类来说,在类D中调用对象value,首先要查找类D中是否有value,如果没有则按顺序查找B->A->C。它是一种广度优先的查找方式,所以首先会在基类A中查找到对象value,然后输出1。

以下是新式类的例子:

# -*- coding:utf-8 -*-
class A(object):
    """
    作为所有类的基类
    """
    value = 1
    
    def __init__(self, ):
        """
        """
        
class B(A):
    """
    D的基类,继承自A
    """
    
    def __init__(self, ):
        """
        """

class C(A):
    """
    D的基类,继承自A
    并且将基类中的变量值修改
    """
    value = 3
    
    def __init__(self, ):
        """
        """
        
class D(B, C):
    """
    """
    
    def __init__(self, ):
        """
        """
        
x = D()
print x.value

对于这个例子输出的结果将是3。

因为基类A继承于object,而所有继承于object的类都将是新式类。对于新式类来说,在类D中调用对象value,它首先查找的依然是类D。如果没有查找到的话,他会依次查找B->C->A。在我看来,它是一种按层查找的原则,会在较低层查找对象,如果没有的话再到较高层查找。所以对于这个例子来说,value在类C中被查找到,所以它的值将会是3。

这种继承方式的查找还是挺有意思的,我本人是比较喜欢新式类的继承方式,因为毕竟如果在继承类中有对于基类中对象的修改,那么这种修改应该是有意义的,而经典类的继承方式就有可能因为基类顺序的问题将这种有意义的修改屏蔽掉,所以还是新式类会比较好一些。而对于python语言的发展来说,python3.0以后所有的类继承方式都将是新式类的继承,也就说所有的类都将是新式类,所以这种继承方式也是我们有必要去掌握的。

zaqwerss 说:
2012年1月26日 20:41

能问下什么事经典类和新式类么?

啊啊啊 说:
2013年3月27日 20:12

经典类就是普通的类, 新式类就是继承于Object的类

neopenx 说:
2015年3月14日 18:33

..BFS和DFS写倒了吧。
旧式是DFS,新式是BFS

전설 서구 说:
2021年2月27日 20:15

Thank you for sharing a bunch of this quality contents, I have bookmarked your blog. Please also explore advice from my site. I will be back for more quality contents. gogoanime

BSEB Matric English 说:
2022年9月27日 07:35

Board of Secondary Education Bihar, Patna board is conducted the state class 10th examination tests as 2nd Indian language for all Hindi, Urdu and Bengali medium students, and the students who have continued their study’s at English medium students have Hindi is the second language and their first language is English, BSEB Matric English Question Paper all students can download BSEB 10th Class English Model Set 2023 Pdf with answer solutions to practice chapter wise important questions.Board of Secondary Education Bihar, Patna board is conducted the state class 10th examination tests as 2nd Indian language for all Hindi, Urdu and Bengali medium students, and the students who have continued their study’s at English medium students have Hindi is the second language and their first language is English.

Emma 说:
2022年12月05日 01:11

In Python, a new-style class is a class that derives from the built-in type `object`. New-style classes were introduced in Python 2.2 and are the default in Python 3.x. Inheritance is a powerful feature in object-oriented programming that allows a child buy cheap diamond rings class to inherit the attributes and methods of its parent class. In Python, a child class can inherit from multiple parent classes, each of which can contribute its own attributes and methods to the child class.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter