Is this code snippet pythonic

Felipe Almeida Lessa felipe.lessa at gmail.com
Mon Apr 10 12:07:07 EDT 2006


Em Seg, 2006-04-10 às 03:52 -0700, jnair at ensim.com escreveu:
> My Tead  Lead  my object counter  code seen below is not  pythonic

As Peter said, you should really ask your Tead Lead, but what about:

class E(object):
    """Holds a class-wide counter incremented when it's instantiated."""
    count = 0
    
    def __init__(self):
        # One instance, increment the counter
        E.count += 1
        

def  test():
    """Test the counter class E."""
    e1 = E()
    assert e1.count == 1
    print e1.count
    
    e2 = E()
    assert e2.count == 2
    print e2.count
    
    e3 = E()
    assert e3.count == 3
    print e3.count

if __name__ == '__main__':
    test()

-- 
Felipe.




More information about the Python-list mailing list