Python中object has no attribute是什么问题?

如题所述

没有继承Object。

首先什么是新式类 经典类呢:

#新式类是指继承object的类

class A(obect):

#经典类是指没有继承object的类

class A:

Python中推荐大家使用新式类 1.新的肯定好哈,已经兼容经典类

2.修复了经典类中多继承出现的bug

下面我们着重说一下多继承的bug 如图:

BC 为A的子类, D为BC的子类 ,A中有save方法,C对其进行了重写

在经典类中 调用D的save方法 搜索按深度优先 路径B-A-C, 执行的为A中save 显然不合理

在新式类的 调用D的save方法 搜索按广度优先 路径B-C-A, 执行的为C中save

#经典类

class A:

def __init__(self):

print 'this is A'

def save(self):

print 'come from A'

class B(A):

def __init__(self):

print 'this is B'

class C(A):

def __init__(self):

print 'this is C'

def save(self):

print 'come from C'

class D(B,C):

def __init__(self):

print 'this is D'

d1=D()

d1.save() #结果为'come from A

#新式类

class A(object):

def __init__(self):

print 'this is A'

def save(self):

print 'come from A'

class B(A):

def __init__(self):

print 'this is B'

class C(A):

def __init__(self):

print 'this is C'

def save(self):

print 'come from C'

class D(B,C):

def __init__(self):

print 'this is D'

d1=D()

d1.save() #结果为'come from C'

Python

(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum于1989年发明,第一个公开发行版发行于1991年。

Python是纯粹的自由软件, 源代码和解释器CPython遵循 GPL(GNU General Public License)协议。Python语法简洁清晰,特色之一是强制用空白符(white space)作为语句缩进。

Python具有丰富和强大的库。它常被昵称为胶水语言,能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。常见的一种应用情形是,使用Python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中有特别要求的部分,用更合适的语言改写,比如3D游戏中的图形渲染模块,性能要求特别高,就可以用C/C++重写,而后封装为Python可以调用的扩展类库。需要注意的是在您使用扩展类库时可能需要考虑平台问题,某些可能不提供跨平台的实现。

7月20日,IEEE发布2017年编程语言排行榜:Python高居首位。

温馨提示:答案为网友推荐,仅供参考
相似回答