473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 1293
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave Benjamin | last post by:
Here are some more ideas for how to implement a statement-friendly code block syntax in Python. Hopefully more "Pythonic" (that is, of or pertaining to those features noticably reminiscent of...
1
by: asdf sdf | last post by:
i need some advice. i'm a back end programmer historically, but have been exploring python for webapps and enjoying it. i need to build some simple Win client-server or standalone apps. the...
16
by: Tran Tuan Anh | last post by:
Dear all: I need your advice on this matter. I am working on a program which takes some pieces of System-C code in and generate some other System-C code. (System-C code is just C++ with some...
11
by: Charles Krug | last post by:
I've a function that needs to maintain an ordered sequence between calls. In C or C++, I'd declare the pointer (or collection object) static at the function scope. What's the Pythonic way to...
4
by: Carl J. Van Arsdall | last post by:
It seems the more I come to learn about Python as a langauge and the way its used I've come across several discussions where people discuss how to do things using an OO model and then how to design...
6
by: jnair | last post by:
My Team Lead says 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():...
2
by: daokfella | last post by:
Hi all, I've created a code snippet in VS 2005 and saved it in the proper snippet folder. When I right-click and select Insert Snippet, I navigation to My Code Snippets and it shows my snippet....
13
by: frk.won | last post by:
I am interested in learning how to use the VS 2005 code snippets. However, I wish to know what are the best ways to source control the code snippets? Are there any source safe/subversion...
4
by: tshad | last post by:
I was watching a video that used a code snippet to create a property and when you type "prop" tab tab, it would create the private variable as well as the property definitions with the private...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.