推荐学习书目
› 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 · Jan 15, 2015 · 6587 views
    This topic created in 4315 days ago, the information mentioned may be changed or developed.

    看这个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 replies  •  2015-01-15 17:13:30 +08:00
    mengzhuo
    mengzhuo
    Jan 15, 2015 via iPhone
    1
    slot
    指定以后 就不能添加其他attr
    最大的作用是省内存
    vJianZhen
    vJianZhen
    OP
    Jan 15, 2015
    2
    @mengzhuo 属于类的么,那么Python/C API有tp_iter、tp_iternext 这两个对象?
    BiggerLonger
    BiggerLonger
    Jan 15, 2015
    3
    当一个类指定了__slot__的时候, 由这个类实例化的对象将不会存在__dict__这个属性, 因此对象无法添加删除属性, 而由于对象少了__dict__属性, 在需要创建大量的这样的对象的时候, 会节省不少内存.
    vJianZhen
    vJianZhen
    OP
    Jan 15, 2015
    4
    @BiggerLonger 你说的的确讲得通。但是放在这几句话里还是觉得很违和,不知道该怎么理解这几句话
    ruoyu0088
    ruoyu0088
    Jan 15, 2015
    5
    vJianZhen
    vJianZhen
    OP
    Jan 15, 2015
    6
    @ruoyu0088 在这里,slot 是字段的意思?
    bjzhush
    bjzhush
    Jan 15, 2015
    7
    既然有人正好问到这个问题,我求指教下.
    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
    skybr
    Jan 15, 2015❤️1
    8
    @bjzhush __slots__是针对new-style class, 你没继承object, 生成的是old-style class
    pheyer
    pheyer
    Jan 15, 2015
    9
    QT里面好像也有一个东东叫slot
    bjzhush
    bjzhush
    Jan 15, 2015
    10
    @skybr 感谢指出
    washinriver
    washinriver
    Jan 15, 2015
    11
    楼上几位说的挺清楚了,我再贴个网上的例子,用slot以减少9g内存空间。
    http://tech.oyster.com/save-ram-with-python-slots/
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   639 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 23:04 · PVG 07:04 · LAX 15:04 · JFK 18:04
    ♥ Do have faith in what you're doing.