Python内置函数Object

Posted stay hungry stay foolish

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python内置函数Object相关的知识,希望对你有一定的参考价值。

英文文档

class object

Return a new featureless object. object is a base for all classes. It has the methods that are common to all instances of Python classes. This function does not accept any arguments.
Note:object does not have a __dict__, so you can’t assign arbitrary attributes to an instance of the object class.

说明:

Object类是Python中所有类的基类,如果定义一个类时没有指定继承那个类,则默认继承object类

>>> class A:
    pass

>>> issubclass(A,object)
True

  

object类定义了所有类的一些公共方法

>>> dir(object)
[\'__class__\', \'__delattr__\', \'__dir__\', \'__doc__\', \'__eq__\', \'__format__\', \'__ge__\', \'__getattribute__\', \'__gt__\', \'__hash__\', \'__init__\', \'__le__\', \'__lt__\', \'__ne__\', \'__new__\', \'__reduce__\', \'__reduce_ex__\', \'__repr__\', \'__setattr__\', \'__sizeof__\', \'__str__\', \'__subclasshook__\']

  

object没有定义__dict__,所以不能对object类实例对象尝试设置属性

>>> a = object()
>>> a.name = \'kim\' # 不能设置属性
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    a.name = \'kim\'
AttributeError: \'object\' object has no attribute \'name\'

#定义一个类A
>>> class A:
    pass

>>> a = A()
>>> 
>>> a.name = \'kim\' # 能设置属性

  

zhuanzai

 

以上是关于Python内置函数Object的主要内容,如果未能解决你的问题,请参考以下文章

Python学习笔记011——内置函数exec()

Python标准库:内置函数type(object)

Python标准库:内置函数delattr(object, name)

Python标准库:内置函数getattr(object, name[, default])

Python内置函数(54)——callable

python内置函数--- hasattrsetattrgetattr