您好老师,为什么我的p1 和p2两个实例都有__dict__属性,内存占用也一样,也能动态绑定,这是什么原因呢

来源:7-2 如何为创建大量实例节省内存

Emproof

2017-12-19

import sys
class player1(object):
    def __init__(self,id,name,status=0,level=1):
        self.id=id
        self.name=name
        self.stat=status
        self.level=level
class player2(object):
    __slot__=['id','name','status','level']
    def __init__(self,id,name,status=0,level=1):
        self.id=id
        self.name=name
        self.stat=status
        self.level=level
set(dir(p1)):set(['__module__', '__str__', '__reduce__', '__dict__', '__sizeof__', '__weakref__', 'id', '__init__', '__setattr__', '__reduce_ex__', '__new__', '__format__', '__class__', '__doc__', 'stat', '__getattribute__', '__subclasshook__', 'name', 'level', '__delattr__', '__repr__', '__hash__'])
set(dir(p2)):set(['__module__', '__str__', '__reduce__', '__dict__', '__sizeof__', '__weakref__', 'id', '__init__', '__setattr__', '__reduce_ex__', '__new__', '__format__', '__class__', '__doc__', '__slot__', 'stat', '__getattribute__', '__subclasshook__', 'name', 'level', '__delattr__', '__repr__', '__hash__'])


写回答

2回答

程序员硕

2017-12-20

__slots__

0
0

Emproof

提问者

2017-12-19

不好意思  输错了 是__slots__ ....

0
0

Python高效编程技巧实战

精选50个Python案例,源自实战,全面提升Python编程能力

2582 学习 · 360 问题

查看课程