V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
1989922yan
V2EX  ›  Python

len()在类中有什么特殊方法? len()一个实例,是 len 了什么的长度呢?

  •  
  •   1989922yan · Dec 2, 2014 · 3431 views
    This topic created in 4168 days ago, the information mentioned may be changed or developed.
    >>> class A(dict): pass
    >>> aa = A()
    >>> len(aa)

    len()在类中有什么特殊方法? len()一个实例,是 len 了什么的长度呢?
    4 replies    2014-12-02 16:32:47 +08:00
    lianghui
        1
    lianghui  
       Dec 2, 2014   ❤️ 2
    __len__ 方法
    aheadlead
        2
    aheadlead  
       Dec 2, 2014   ❤️ 1
    1L正解...
    staticor
        3
    staticor  
       Dec 2, 2014   ❤️ 1
    可参考http://stackoverflow.com/questions/2481421/difference-between-len-and-len 或者定义一个类后改写一下 __len__ 方法
    joshryo
        4
    joshryo  
       Dec 2, 2014   ❤️ 1
    len函数会去调用类的__len__方法,所以想要能用len函数,你定义类的时候都得写__len__方法
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2509 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 12:19 · PVG 20:19 · LAX 05:19 · JFK 08:19
    ♥ Do have faith in what you're doing.