标签:repr ror ble 提前 _for opened time last mil
>>> p1 = player(‘0001‘,‘Jim‘) >>> p2 = player2(‘0001‘,‘Jim‘)
创建两个对象p1和p2,其中p1要比p2大
>>> help(dir) Help on built-in function dir in module __builtin__: dir(...) dir([object]) -> list of strings If called without an argument, return the names in the current scope. Else, return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it. If the object supplies a method named __dir__, it will be used; otherwise the default dir() logic is used and returns: for a module object: the module‘s attributes. for a class object: its attributes, and recursively the attributes of its bases. for any other object: its attributes, its class‘s attributes, and recursively the attributes of its class‘s base classes.
>>> dir(p1) [‘__class__‘, ‘__delattr__‘, ‘__dict__‘, ‘__doc__‘, ‘__format__‘, ‘__getattribute__‘, ‘__hash__‘, ‘__init__‘, ‘__module__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘__weakref__‘, ‘level‘, ‘name‘, ‘stat‘, ‘uid‘] >>> dir(p2) [‘__class__‘, ‘__delattr__‘, ‘__doc__‘, ‘__format__‘, ‘__getattribute__‘, ‘__hash__‘, ‘__init__‘, ‘__module__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__slots__‘, ‘__str__‘, ‘__subclasshook__‘, ‘level‘, ‘name‘, ‘stat‘, ‘uid‘]
>>> set(dir(p1)) - set(dir(p2)) #将其发生转为集合后,再执行差集 set([‘__dict__‘, ‘__weakref__‘]) #多了两个属性,在不使用弱引用时,__weakref__属性很小,忽略不计,责任落在了__dict__属性上 >>>
>>> p1.__dict__ {‘stat‘: 0, ‘level‘: 1, ‘uid‘: ‘0001‘, ‘name‘: ‘Jim‘}
>>> p1.x Traceback (most recent call last): File "<pyshell#14>", line 1, in <module> p1.x AttributeError: ‘player‘ object has no attribute ‘x‘ >>> p1.x = 123 >>> p1.x 123 >>> p1.__dict__ {‘x‘: 123, ‘stat‘: 0, ‘level‘: 1, ‘uid‘: ‘0001‘, ‘name‘: ‘Jim‘} >>> p1.__dict__[‘y‘] = 99 >>> p1.y 99
>>> del p1.__dict__[‘x‘] >>> p1.__dict__ {‘stat‘: 0, ‘uid‘: ‘0001‘, ‘level‘: 1, ‘y‘: 99, ‘name‘: ‘Jim‘}
>>> import sys >>> sys.getsizeof(p1.__dict__) 524
__slots__ = [‘uid‘, ‘name‘, ‘stat‘, ‘level‘]
标签:repr ror ble 提前 _for opened time last mil
原文地址:https://www.cnblogs.com/smulngy/p/9008306.html