python中class(object)的含义是什么以及用法

作者:Nicola-Zhang 时间:2023-08-07 22:00:19 

python class(object)的含义

在python2中有区别,在Python3中已经没有区别:

object为默认类,表示继承关系

class Person:
    name = "zhengtong"

class Animal(object):
    name = "chonghong"
 
if __name__ == "__main__":
    x = Person()
    print( "Person", dir(x))
 
    y = Animal()
    print ("Animal", dir(y))

Python3中运行结果:

person [‘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’, ‘name’]
animal [‘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’, ‘name’]

class, class()和class(object)的区别

为什么要继承object类

Python2中,遇到 class A 和 class A(object) 是有概念上和功能上的区别的,分别称为经典类(旧式类,old-style)与新式类(new-style)的区别。python2中为什么在进行类定义时最好要加object,加 & 不加如下实例。

历史进程:2.2以前的时候type和object还不统一. 在2.2统一到3之间, 要用class

  • Foo(object)来申明新式类, 因为它的type是 < type &lsquo;type&rsquo; > 。

  • 不然的话, 生成的类的type就是 <type &lsquo;classobj&rsquo; >。

继承object类的原因:主要目的是便于统一操作。

  • 在python 3.X中已经默认继承object类。

# -.- coding:utf-8 -.-
# __author__ = 'zhengtong'

class Person:
   """
   不带object
   """
   name = "zhengtong"

class Animal(object):
   """
   带有object
   """
   name = "chonghong"

if __name__ == "__main__":
   x = Person()
   print "Person", dir(x)

y = Animal()
   print "Animal", dir(y)

Person ['__doc__', '__module__', 'name']
Animal ['__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变量, 也就是说这个类的命名空间只有三个对象可以操作。

Animal类继承了object对象,拥有了好多可操作对象,这些都是类中的高级特性。

class, class()和class(object)的区别

python2中写为如下两种形式都是不能继承object类的,也就是说是等价的。

def class:
def class():

继承object类是为了让自己定义的类拥有更多的属性,以便使用。当然如果用不到,不继承object类也可以。

python2中继承object类是为了和python3保持一致,python3中自动继承了object类。

python2中需要写为如下形式才可以继承object类。

def class(object):

来源:https://blog.csdn.net/yangwangnndd/article/details/89384389

标签:python,class,object
0
投稿

猜你喜欢

  • python SMTP实现发送带附件电子邮件

    2023-09-29 11:31:54
  • django获取from表单multiple-select的value和id的方法

    2021-11-03 13:13:31
  • 用mysql做站点时怎样记录未知错误的发生

    2009-01-14 13:16:00
  • Python开发装包八种方法详解

    2021-01-12 22:27:28
  • Python3环境安装Scrapy爬虫框架过程及常见错误

    2021-10-19 00:01:05
  • Python中图像算术运算的示例详解

    2023-01-01 19:56:43
  • python实现对excel中需要的数据的单元格填充颜色

    2023-03-04 13:02:39
  • python绘制条形图方法代码详解

    2022-11-30 00:07:19
  • SQL server 2005中设置自动编号字段的方法

    2024-01-12 13:55:47
  • python实现图片批量压缩

    2022-01-20 03:03:20
  • 基于Python+Pygame实现变异狗大战游戏

    2021-04-19 09:46:45
  • Python计算素数个数的两种方法

    2023-09-09 16:38:19
  • 仿谷歌主页js动画效果实现代码

    2024-04-17 10:07:36
  • 如何根据用户银行帐户余额的多少进行显式的提交或终止?

    2009-11-22 19:28:00
  • python3+PyQt5 实现Rich文本的行编辑方法

    2023-09-29 14:37:47
  • python matplotlib库绘制散点图例题解析

    2023-08-14 03:12:56
  • mysql中find_in_set()函数的使用及in()用法详解

    2024-01-25 18:32:38
  • Python基于mysql实现学生管理系统

    2024-01-24 05:57:47
  • 详解vue页面首次加载缓慢原因及解决方案

    2024-05-25 15:19:45
  • 跟老齐学Python之正规地说一句话

    2022-10-15 06:49:29
  • asp之家 网络编程 m.aspxhome.com