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 · 5278 次点击
    这是一个创建于 3391 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看这个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   ·   我们的愿景   ·   实用小工具   ·   5424 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:35 · PVG 16:35 · LAX 01:35 · JFK 04:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.