美文网首页
python2中为什么在进行类定义时最好要加object,不加又

python2中为什么在进行类定义时最好要加object,不加又

作者: 青哥DevOps | 来源:发表于2018-10-22 22:31 被阅读0次
# -.- 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对象,拥有了好多可操作对象,这些都是类中的高级特性。

原文地址:https://blog.csdn.net/w571523631/article/details/55099385

相关文章

网友评论

      本文标题:python2中为什么在进行类定义时最好要加object,不加又

      本文链接:https://www.haomeiwen.com/subject/cjvnzftx.html