Python新式类和旧式类的区别

Python2和Python3中的新式类、经典类(旧式类)的区别

基本概念

python2.x中默认都是经典类,只有显示继承了object的才是新式类

class Person(object):  # 新式类
	pass
class Person():  # 经典类
	pass
class Person:  # 经典类
	pass

python3.x中取消了经典类,默认都是新式类,不用继承object也是新式类

# 都是新式类写法,并无区别
class Person(object):
	pass
class Person():
	pass
class Person:
	pass

多继承的调用顺序

以下代码是python2.x中的

经典类多继承搜索顺序(深度优先):当继承多个父类的时候从左到右深度搜索需要的属性,如果父类没有,就去父类的父类中查找,如果没有再从继承的下一个父类中查找。深度优先就是把一个继承的父类一直往深度去查找。

	class A:
        def __init__(self):
            self.n = "A"
    
    class B(A):
        def __init__(self):
            super(B, self).__init__()
            self.n = "B"
    
    class C(A):
        def __init__(self):
            self.n = "C"
    
    class D(B, C):
        def __init__(self):
            super(D, self).__int__()
            self.b = "D"
    
    d = D()
    print(d.n)  # 输出D

注释D中的代码,获得B

	class A:
        def __init__(self):
            self.n = "A"
    
    class B(A):
        def __init__(self):
            super(B, self).__init__()
            self.n = "B"
    
    class C(A):
        def __init__(self):
            self.n = "C"
    
    class D(B, C):
    	pass
        # def __init__(self):
        #    super(D, self).__int__()
        #    self.b = "D"
    
    d = D()
    print(d.n)  # 输出B

注释B中的代码,获得A

	class A:
        def __init__(self):
            self.n = "A"
    
    class B(A):
    	pass
        # def __init__(self):
        #    super(B, self).__init__()
        #   self.n = "B"
    
    class C(A):
        def __init__(self):
            self.n = "C"
    
    class D(B, C):
    	pass
        # def __init__(self):
        #    super(D, self).__int__()
        #    self.b = "D"
    
    d = D()
    print(d.n)  # 输出A

注释A中的代码,获得C

	class A:
        pass
        # def __init__(self):
        #   self.n = "A"
    
    class B(A):
    	pass
        # def __init__(self):
        #    super(B, self).__init__()
        #   self.n = "B"
    
    class C(A):
        def __init__(self):
            self.n = "C"
    
    class D(B, C):
    	pass
        # def __init__(self):
        #    super(D, self).__int__()
        #    self.b = "D"
    
    d = D()
    print(d.n)  # 输出C

新式类多继承的搜索顺序(广度优先):当继承多个父类的时候从左到右广度搜索需要的属性,如果父类没有,就去继承的下一个父类中查找,广度优先其实就是广泛的从继承的父类中查找,不往深度去查找。

	class A(object):
        def __init__(self):
            self.n = "A"
    
    class B(A):
        def __init__(self):
            super(B, self).__init__()
            self.n = "B"
    
    class C(A):
        def __init__(self):
            self.n = "C"
    
    class D(B, C):
        def __init__(self):
            super(D, self).__int__()
            self.b = "D"
    
    d = D()
    print(d.n)  # 输出D

注释D中的代码获得B

	class A(object):
        def __init__(self):
            self.n = "A"
    
    class B(A):
        def __init__(self):
            super(B, self).__init__()
            self.n = "B"
    
    class C(A):
        def __init__(self):
            self.n = "C"
    
    class D(B, C):
    	pass
        # def __init__(self):
        #    super(D, self).__int__()
        #    self.b = "D"
    
    d = D()
    print(d.n)  # 输出B

注释B中的代码获得C

	class A(object):
        def __init__(self):
            self.n = "A"
    
    class B(A):
    	pass
        # def __init__(self):
        #    super(B, self).__init__()
        #   self.n = "B"
    
    class C(A):
        def __init__(self):
            self.n = "C"
    
    class D(B, C):
    	pass
        # def __init__(self):
        #    super(D, self).__int__()
        #    self.b = "D"
    
    d = D()
    print(d.n)  # 输出C

注释C中的代码获得A

	class A(object):
        def __init__(self):
            self.n = "A"
    
    class B(A):
    	pass
        # def __init__(self):
        #    super(B, self).__init__()
        #   self.n = "B"
    
    class C(A):
        # def __init__(self):
        #   self.n = "C"
    
    class D(B, C):
    	pass
        # def __init__(self):
        #    super(D, self).__int__()
        #    self.b = "D"
    
    d = D()
    print(d.n)  # 输出A

多继承

class Food(object):

     def __init__(self, name, color):
          self.name = name
          self.color = color
  
     def eatable(self):
          print("%s can be eaten." % self.name)
  
     def appearance(self):
         print('The color of the %s is %s.' % (self.name, self.color))
 
 
class Fruits(object):
 
     def __init__(self, name, nutrition):
         self.name = name
        self.nutrition = nutrition
 
     def info(self):
         print("%s can supply much %s." % (self.name, self.nutrition))
 
 
class Salad(Fruits, Food):  # 继承多个父类
 
     def __init__(self, name, nutrition, color, tasty):
         super(Salad, self).__init__(name, nutrition)
         Food.__init__(self, name, color)
         self.tasty = tasty
 
     def taste(self):
        print("%s is a little %s." % (self.name, self.tasty))
 
 
 obj = Salad('orange', 'VC', 'orange', 'sour') 
 obj.eatable()
 obj.appearance()
 obj.info()
 obj.taste()

supper()函数为新式类的方法,采用新式类要求最顶层的父类一定要继承于object,这样就可以用super()函数来调用父类的init()等函数。每个父类都执行且执行一次,并不会出现重复调用的情况。采用super()方法时,会自动找到第一个多继承中的第一父类。
  但是如果想要继续调用其它父类init()函数或两个父类的同名函数时,就要用经典类的调用方法了,即 父类名.init(self,参数),如上例

总结

1、新式类继承object类,经典类不继承任何类

2、新式类用super关键字继承构造方法,经典类用 父类.init(self)来继承

3、新式类:广度优先查询,经典类:深度优先查询(因为新式类讲究的是新,所以要找最近的,最新的;然后经典的讲究古老,所以更远更深的)

4、值得注意的是,上面是在python2中做的,在python3中不管是经典类还是新式类,都是采用的是广度优先查询,已经废弃2中的深度查询了

你可能感兴趣的:(Python基础)