前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python中的类在声明的时候继承object的原因

Python中的类在声明的时候继承object的原因

作者头像
知识浅谈
发布2020-06-16 15:30:45
1.3K0
发布2020-06-16 15:30:45
举报
文章被收录于专栏:分享学习分享学习
代码语言:javascript
复制
首先说明python3中无论写不写(object),默认的会自动带上,所以在python3中写不写都一样
class Student1:
    pass

class Student2(object):
    pass
print(dir(Student1()))
print(dir(Student2()))
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
可以看出两个的所具有相同的属性,因为继承了object
若不继承则只有__doc__ , __module__ 这两个属性,因为上边的属性,我们都要使用,所以一般是继承的。
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-06-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档