Python内置函数Object

Python Object 类详解
本文介绍了Python中的Object类作为所有类的基础角色,详细解释了Object类的特性与限制,包括其提供的通用方法及为何不能为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

 

转载于:https://www.cnblogs.com/yyf573462811/p/9591678.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值