python class和class(object)用法区别

转自:

https://www.cnblogs.com/liulipeng/p/7069004.html

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

# -*- coding: utf-8 -*-

# 经典类或者旧试类


class A:
    pass


a = A()


# 新式类

class B(object):
    pass


b = B()

# python2不支持
# print(A.__class__)
print(a.__class__)
print(type(A))
print(type(a))

# python2
# __main__.A
# <type 'classobj'>
# <type 'instance'>

# python3
# <class 'type'>
# <class '__main__.A'>
# <class 'type'>
# <class '__main__.A'>

print(B.__class__)
print(b.__class__)
print(type(B))
print(type(b))

# python2
# <type 'type'>
# <class '__main__.B'>
# <type 'type'>
# <class '__main__.B'>

# python3
# <class 'type'>
# <class '__main__.B'>
# <class 'type'>
# <class '__main__.B'>


# 旧式类的实现不够好,类是类,实例是实例,类的类型是classobj,实例的类型是instance,两者的联系只在于__class__,
# 这和内置对象是不同的,int对象的类型就是int,同时int()返回的也是int类型的对象,内置对象和自定义对象不同就对代码统一实现带来很大困难。
#
# 新式类
#
# 1. 所有类的类型都是type
# 2. 所有类调用的结果都是构造,返回这个类的实例
# 3. 所有类都是object的子类
# 4. 新式类不仅可以用旧类调用父类的方法,也可以用super方法。

复制代码

那写object和不写object有什么区别?

好的,再用代码来理解它们的区别.

 
  1. # -.- coding:utf-8 -.-

  2. # __author__ = 'zhengtong'

  3.  
  4.  
  5. class Person:

  6.     """

  7.     不带object

  8.     """

  9.     name = "zhengtong"

  10.  
  11.  
  12. class Animal(object):

  13.     """

  14.     带有object

  15.     """

  16.     name = "chonghong"

  17.  
  18. if __name__ == "__main__":

  19.     x = Person()

  20.     print "Person", dir(x)

  21.  
  22.     y = Animal()

  23.     print "Animal", dir(y)

 

运行结果

 
  1. Person ['__doc__', '__module__', 'name']

  2. Animal ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', 

  3. '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 

  4. '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']

 

 

Person类很明显能够看出区别,不继承object对象,只拥有了__doc__ , __module__ 和 自己定义的name变量, 也就是说这个类的命名空间只有三个对象可以操作.

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

 

对于不太了解python类的同学来说,这些高级特性基本上没用处,但是对于那些要着手写框架或者写大型项目的高手来说,这些特性就比较有用了,比如说tornado里面的异常捕获时就有用到__class__来定位类的名称,还有高度灵活传参数的时候用到__dict__来完成.

 

最后需要说清楚的一点, 本文是基于python 2.7.10版本,实际上在python 3 中已经默认就帮你加载了object了(即便你没有写上object)。

 

这里附上一个表格用于区分python 2.x 和 python 3.x 中编写一个class的时候带上object和不带上object的区别.

python 2.xpython 2.xpython 3.xpython 3.x
不含object含object不含object含object
__doc____doc____doc____doc__
__module____module____module____module__
say_hellosay_hellosay_hellosay_hello
 __class____class____class__
 __delattr____delattr____delattr__
 __dict____dict____dict__
 __format____format____format__
 __getattribute____getattribute____getattribute__
 __hash____hash____hash__
 __init____init____init__
 __new____new____new__
 __reduce____reduce____reduce__
 __reduce_ex____reduce_ex____reduce_ex__
 __repr____repr____repr__
 __setattr____setattr____setattr__
 __sizeof____sizeof____sizeof__
 __str____str____str__
 __subclasshook____subclasshook____subclasshook__
 __weakref____weakref____weakref__
  __dir____dir__
  __eq____eq__
  __ge____ge__
  __gt____gt__
  __le____le__
  __lt____lt__
  __ne____ne__
  • 12
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值