class Sample(object):
_count = 0
def __init__(self):
Sample._count += 1
@property
def count(self):
return Sample._count
Usage is slightly different from Ruby; for example, if you have this code in a module a.py,
>>> import a
>>> x = a.Sample()
>>> print x.count
1
>>> y = a.Sample()
>>> print x.count
2
having a class property Sample.count (with the same name as the instance property) will be a bit complicated in Python (perhaps, but don't bother IMHO).
source
share