Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is this code snippet pythonic

Reply
Thread Tools

Is this code snippet pythonic

 
 
jnair@ensim.com
Guest
Posts: n/a
 
      04-10-2006
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

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      04-10-2006
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

 
Reply With Quote
 
 
 
 
Kent Johnson
Guest
Posts: n/a
 
      04-10-2006
Peter Hansen wrote:
> 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
 
Reply With Quote
 
Felipe Almeida Lessa
Guest
Posts: n/a
 
      04-10-2006
Em Seg, 2006-04-10 Ã*s 03:52 -0700, 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object Oriented vs Pythonic Code, and Pythonic standards Carl J. Van Arsdall Python 4 02-07-2006 10:15 PM
Code snippets, tool to convert basic to .snippet xml? Edwin Knoppert ASP .Net 0 11-30-2005 03:45 PM
Is there a code snippet showing how to do a Redirect on a TreeView event Tom ASP .Net 3 12-01-2004 03:04 AM
Request Handler code snippet wanted plz Gil Blais Java 0 04-15-2004 08:23 AM
How can I save my code snippet on toolbox in vs.net? Clare Hsiao ASP .Net 0 02-12-2004 02:01 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57