python2中为什么在进行类定义时最好要加object
生活随笔
收集整理的這篇文章主要介紹了
python2中为什么在进行类定义时最好要加object
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
class Student(object):def __init__(self, name, score):self.name = nameself.score = scoredef print_score(self):print('%s: %s' % (self.name, self.score))lg=Student('lg',99)lg.namelg.print_score()class Person:"""不帶object"""name = "zhengtong"class Animal(object):"""帶有object"""name = "chonghong"if __name__ == "__main__":x = Person()print ("Person", len(dir(x)))print ("Person", dir(x))y = Animal()print ("Animal", len(dir(y)))print ("Animal", dir(y))
lg: 99
Person 27
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 27
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']
object是一個基類,或稱之為元類。在Python3 中加不加一個樣
總結
以上是生活随笔為你收集整理的python2中为什么在进行类定义时最好要加object的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pytorch 入门(二) cnn 手写
- 下一篇: Python类访问限制