栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python class(object)中object是做什么的

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python class(object)中object是做什么的

python3中无区别

首先,在python3中加不加object没有区别,因为实际上在python 3 中已经默认就帮你加载了object了,但是在python2中有区别。

python2中区别

示例:

class Person:  #无object
    name = "zhangsan"
    
class Dog(object): #有object
    name = "pipi"
    
if __name__ == "__main__":
    x = Person()
    print "Person", dir(x) 
    y = Dog()
    print "Dog", dir(y)

结果:

Person ['__doc__', '__module__', 'name']
Dog ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', 
'__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']

显而易见,Person类不继承object对象,只拥有了__doc__, __module__ 和 自己定义的name变量, 也就是说这个类的命名空间只有三个对象可以操作;
Dog类继承了object对象,拥有了好多可操作对象,这些都是类中的高级特性。

参考:https://blog.csdn.net/DeepOscar/article/details/80947155

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/357247.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号