469,923 Members | 1,355 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

Is this code snippet pythonic

My Tead Lead my object counter code seen below is not pythonic

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

def test():
if __name__ == "__main__":
e1 = E()
print e1.count
e2 = E()
print e2.count
e3 = E()
print e3.count

test()

if not waht woutld be the pythonic way

Apr 10 '06 #1
3 1209
jn***@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

Apr 10 '06 #2
Peter Hansen wrote:
jn***@ensim.com wrote:
class E(object): _count = 0
def __init__(self):
E._count += 1
count = property(lambda self: E._count )

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


Yes he does, it's a property getter, it will be called with self as an
argument.

Kent
Apr 10 '06 #3
Em Seg, 2006-04-10 *s 03:52 -0700, jn***@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.

Apr 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Dave Benjamin | last post: by
1 post views Thread by asdf sdf | last post: by
16 posts views Thread by Tran Tuan Anh | last post: by
11 posts views Thread by Charles Krug | last post: by
4 posts views Thread by Carl J. Van Arsdall | last post: by
6 posts views Thread by jnair | last post: by
2 posts views Thread by daokfella | last post: by
4 posts views Thread by tshad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.