Is this code snippet pythonic

Peter Hansen peter at engcorp.com
Mon Apr 10 10:45:43 EDT 2006


jnair at ensim.com wrote:
> My Tead  Lead  my object counter  code seen below is not  pythonic

I'm guessing this was supposed to say "My team lead says my ...." (?)

>  class E(object):
>     _count = 0
>     def __init__(self):
>         E._count += 1
>     count = property(lambda self: E._count )

Is this supposed to work in a threaded environment?  If so, you'll at 
least need to add an acquire/release pair using a threading.Lock()...

> if  not  waht woutld be the  pythonic way

Other comments:

1. Why the property?  Can't you just access ._count directly?

2. You don't need the "self" in the lambda, since you're not using it 
anyway.

3. There aren't any comments.  At the least there ought to be one above 
"_count = 0" telling the reader what the variable is for.

4. Why are you asking us?  The term "pythonic" doesn't have a fixed 
definition, so asking your "Tead Lead" makes more sense.  From us you 
might learn something, but the result might still not satisfy the one 
person you need to satisfy with this.  Maybe all he wants is to see a 
getter method instead of the lambda...

-Peter




More information about the Python-list mailing list