推荐学习书目
› 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
Hualin
V2EX  ›  Python

求助 - 如何在 python 类里面建一个计数器

  •  
  •   Hualin · Aug 29, 2012 · 5114 views
    This topic created in 5152 days ago, the information mentioned may be changed or developed.
    请教大家,

    如何在 python 类里面建一个计数器,以后每建一个这个类的对象,这个类的对象有个成员 self.ID,用来记录在运行期间到底生成过多少该类的对象,就像 java 中的静态成员变量。好久不用咯 =。=
    2 replies  •  1970-01-01 08:00:00 +08:00
    Hualin
        1
    Hualin  
    OP
       Aug 29, 2012
    搞明白了。。。。

    ==code==
    class A(object):
    id = 0
    def __init__(self):
    A.id +=1
    ==code==
    zztczcx
        2
    zztczcx  
       Aug 29, 2012
    嗯 没错~就是类属性。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   6135 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 02:36 · PVG 10:36 · LAX 19:36 · JFK 22:36
    ♥ Do have faith in what you're doing.