Python 特有的?什么是 slot - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
vJianZhen
V2EX    Python

Python 特有的?什么是 slot

  •  
  •   vJianZhen 2015-01-15 15:02:59 +08:00 5838 次点击
    这是一个创建于 3973 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看这个Python文档的这部分: https://docs.python.org/3/library/stdtypes.html#typeiter

    里面关于迭代器类型,有几个地方提到了slot这个东西:

    1.This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.
    2.This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.
    3.This method corresponds to the tp_iternext slot of the type structure for Python objects in the Python/C API.

    就是每个方法讲完,最后都有这么一句。那么这些句子怎么理解,特别是slot是什么东西!求解释或相关资料。

    谷歌之后也没找到答案。

    11 条回复    2015-01-15 17:13:30 +08:00
    mengzhuo
        1
    mengzhuo  
       2015-01-15 15:09:01 +08:00 via iPhone
    slot
    指定以后 就不能添加其他attr
    最大的作用是省内存
    vJianZhen
        2
    vJianZhen  
    OP
       2015-01-15 15:10:45 +08:00
    @mengzhuo 属于类的么,那么Python/C API有tp_iter、tp_iternext 这两个对象?
    BiggerLonger
        3
    BiggerLonger  
       2015-01-15 15:16:25 +08:00
    当一个类指定了__slot__的时候, 由这个类实例化的对象将不会存在__dict__这个属性, 因此对象无法添加删除属性, 而由于对象少了__dict__属性, 在需要创建大量的这样的对象的时候, 会节省不少内存.
    vJianZhen
        4
    vJianZhen  
    OP
       2015-01-15 15:27:58 +08:00
    @BiggerLonger 你说的的确讲得通。但是放在这几句话里还是觉得很违和,不知道该怎么理解这几句话
    ruoyu0088
        5
    ruoyu0088  
       2015-01-15 15:54:36 +08:00
    vJianZhen
        6
    vJianZhen  
    OP
       2015-01-15 16:23:52 +08:00
    @ruoyu0088 在这里,slot 是字段的意思?
    bjzhush
        7
    bjzhush  
       2015-01-15 16:34:33 +08:00
    既然有人正好问到这个问题,我求指教下.
    py2.7中,slot不生效,不知道是我写的有问题还是什么问题呢..
    code:

    class Rog():
    __slots__ = ('name', 'age')
    pass

    r = Rog()
    r.xxx = 'xxxval'
    print r.xxx

    执行结果
    zs@zsLinux:~/python$ python /tmp/x.py
    xxxval

    python版本
    zs@zsLinux:~/python$ python
    Python 2.7.3 (default, Feb 27 2014, 19:58:35)
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>

    求指教,为什么slot不生效呢?
    skybr
        8
    skybr  
       2015-01-15 16:44:35 +08:00   1
    @bjzhush __slots__是针对new-style class, 你没继承object, 生成的是old-style class
    pheyer
        9
    pheyer  
       2015-01-15 16:46:55 +08:00
    QT里面好像也有一个东东叫slot
    bjzhush
        10
    bjzhush  
       2015-01-15 17:09:28 +08:00
    @skybr 感谢指出
    washinriver
        11
    washinriver  
       2015-01-15 17:13:30 +08:00
    楼上几位说的挺清楚了,我再贴个网上的例子,用slot以减少9g内存空间。
    http://tech.oyster.com/save-ram-with-python-slots/
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     985 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 18:56 PVG 02:56 LAX 10:56 JFK 13:56
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86