objectpython
1. python中的object类有何作用
object是一个基类,或称之为元类。
在python2.x上,不继承object类的称之为经典类,继承了object类的称之为新式类
关于它们的区别,你可以阅读以下python2手册中的这个部分:
https://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes
2. 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'
3. python的class中的object是什么意思
object 是指这个类继承的最顶级的对象。python3.x 中已经可以省略object,可直接
classSample():
pass
4. Python 的 type 和 object 之间是怎么一种关系
Python的object和type理解
1、节选自Python Documentation 3.5.2的部分解释
Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann’s model of a “stored program computer,” code is also represented by objects.)
对象是Python对数据的抽象。 Python程序中的所有数据都由对象或对象之间的关系表示。(在某种意义上,并且符合冯·诺依曼的“存储程序计算机”的模型,代码也由对象表示的)。
Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘is‘ operator compares the identity of two objects; the id() function returns an integer representing its identity.
每个对象都有一个标识,一个类型和一个值。 对象的身份一旦创建就不会改变; 你可以把它看作内存中的对象地址。'is'运算符比较两个对象的标识; id()函数返回一个表示其身份的整数。
An object’s type determines the operations that the object supports (e.g., “does it have a length?”) and also defines the possible values for objects of that type. The type() function returns an object’s type (which is an object itself). Like its identity, an object’s type is also unchangeable.
对象的类型决定对象支持的操作(例如,“它有长度吗?”),并且还定义该类型对象的可能值。type()函数返回一个对象的类型(它是一个对象本身)。与它的身份一样,对象的类型也是不可改变的。
2、Pyhtml的解释:
object:
class object
The most base type
type:
class type(object)
type(object_or_name, bases, dict)
type(object) -> the object's type
type(name, bases, dict) -> a new type
从上面三个图可以看出,对象obeject是最基本的类型type,它是一个整体性的对数据的抽象概念。相对于对象object而言,类型type是一个稍微具体的抽象概念,说它具体,是因为它已经有从对象object细化出更具体抽象概念的因子,这就是为什么type(int)、type(float)、type(str)、type(list)、type(tuple)、type(set)等等的类型都是type,这也是为什么instance(type, object)和instance(object, type)都为True的原因,即类型type是作为int、float等类型的整体概念而言的。那么,为什么issubclass(type, object)为True,而issubclass(object, type)为Flase呢?从第二张图,即从继承关系可以看到,type是object的子类,因此前者为True,后者为False。若从Python语言的整体设计来看,是先有对象,后有相对具体的类型,即整体优先于部分的设计思想。
如果从更加本质的视角去看待这些问题的话,就要从Python Documentation-->3. Data Model-->3.1 Objects,values and types找原因了[请参考Python官方标准库],从标准库里可以看到:
object是Python对数据的抽象,它是Python程序对数据的集中体现。
每个对象都有一个标识,一个类型和一个值。
对象的类型决定对象支持的操作。
某些对象的值可以更改。 其值可以改变的对象称为可变对象;对象的值在创建后不可更改的对象称为不可变对象。
因此,从Python整体设计体系来看的话,就是先有对象,再有标识、类型和值,接着是对对象的操作等等,这也就解释了图3的结果形成的原因了。
5. python(unsubscriptable object异常)
python(unsubscriptable object异常),是设置错误造成的,解决方法如下;
1、首先创建一个py文件,输入“for i in range(10):y=1if i==5:y=0i=i/yprint(i)”代码,如下图所示。
6. python 添加了这个奇怪的叫做 object 的 class,它究竟有什么含义
在Python里,有一句话叫,一切皆对象。而对象的英文单词就是object。
换句话这个object就代表了Python中的一切,开发Python的龟叔已经给object类定义常用的属性和方法供认识使用。
当在Python2中,你定义类的时候,加上object,用内置函数dir(你定义类的名字)查看属性和方法,你定义的类会默认继承object这个基类(父类)的属性和方法。如果不给object参数,dir时就只有你自己定义属性和方法,没有继承object的。
你可以下去动手试试(我是手机端回答的问题,抱歉无法演示截图)
然后,如果你使用的是Python3,当你定义类的时候,给了object参数就继承。不给object参数也会默认继承。不管你是
class A:
还是 class A():
都会自动的被Python识别为:
class A(object)(这也是Python3更人性化的一点)
7. Python 为什么要继承 object 类
没有规定必须继承OBJECT类。OBJECT类只是面向对象语言继承精神的一种表现。例如OBJECTC,C++等语言在标准库中都将所有类的基类定义位OBJECT,这样的好处就是最大限度的利用代码重用的的精神。但是不通语言中的OBJECT类的作用又很不一样所以看看下面的介绍:回到PYTHON中为什么要集成OBJECT:low-levelconstructorsnamed__new__()–低级别的构造函数.Note:Python的class__init__并不是其他语言意义上的构造函数,在new创建实例后对实例属性初始化的函数.descriptors,–描述符.或者说描述符协议支持.descriptorprotocol__get__,__set__,__delete__等,可以阅读descriptor文档staticmethodsandclassmethods-静态方法和类方法properties(computedattributes)–属性访问settergetter.decorators(introcedinPython2.4)–装饰器.现在装饰器语法糖遍布各Python框架.slots–用户设置后可以限定实例的属性.在Python2中替代__dict__,可以节省近2/3内存,Python3中可以不因为优化内存使用率而使用slots,因为__dict__结构内存做了优化,Note:__dict__并不是Python意义上的内置的dict,其实是一个proxy类.anewMethodResolutionOrder(MRO)–MRO方法解析次序改变(由左递归改为C3算法)可能上面的你看着不太理解。通俗说一下py2.2后继承object的目的是使这个类成为newstyleclass,没有继承object的为传统classicclass,在本机进行了测试,环境为py2.7.3classFoo(object):passclassFoo1:passprinttype(Foo),type(Foo1)printdir(Foo)printdir(Foo1)printisinstance(Foo,object)printisinstance(Foo1,object)结果如下:['__class__','__delattr__','__dict__','__doc__','__format__','__getattribute__','__hash__','__init__','__mole__','__new__','__rece__','__rece_ex__','__repr__','__setattr__','__sizeof__','__str__','__subclasshook__','__weakref__']['__doc__','__mole__']TrueTrue(这个True有些疑问,Foo1不应是object的实例啊)
8. python 如何查看object有哪些属性值
dir([obj]):
调用这个方法将返回包含obj大多数属性名的列表(会有一些特殊的属性不包含在内)。obj的默认值是当前的模块对象。
hasattr(obj, attr):
这个方法用于检查obj是否有一个名为attr的值的属性,返回一个布尔值。
getattr(obj, attr):
调用这个方法将返回obj中名为attr值的属性的值,例如如果attr为’bar’,则返回obj.bar。
setattr(obj, attr, val):
调用这个方法将给obj的名为attr的值的属性赋值为val。例如如果attr为’bar’,则相当于obj.bar = val。