Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old April 10th, 2006, 12:05 PM
jnair@ensim.com
Guest
 
Posts: n/a
Default 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

  #2  
Old April 10th, 2006, 03:55 PM
Peter Hansen
Guest
 
Posts: n/a
Default Re: Is this code snippet pythonic

jnair@ensim.com wrote:[color=blue]
> My Tead Lead my object counter code seen below is not pythonic[/color]

I'm guessing this was supposed to say "My team lead says my ...." (?)
[color=blue]
> class E(object):
> _count = 0
> def __init__(self):
> E._count += 1
> count = property(lambda self: E._count )[/color]

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()...
[color=blue]
> if not waht woutld be the pythonic way[/color]

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

  #3  
Old April 10th, 2006, 04:25 PM
Kent Johnson
Guest
 
Posts: n/a
Default Re: Is this code snippet pythonic

Peter Hansen wrote:[color=blue]
> jnair@ensim.com wrote:[color=green][color=darkred]
>>> class E(object):[/color]
>> _count = 0
>> def __init__(self):
>> E._count += 1
>> count = property(lambda self: E._count )[/color][/color]
[color=blue]
> 2. You don't need the "self" in the lambda, since you're not using it
> anyway.[/color]

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

Kent
  #4  
Old April 10th, 2006, 05:15 PM
Felipe Almeida Lessa
Guest
 
Posts: n/a
Default Re: Is this code snippet pythonic

Em Seg, 2006-04-10 Ã*s 03:52 -0700, jnair@ensim.com escreveu:[color=blue]
> My Tead Lead my object counter code seen below is not pythonic[/color]

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.

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles