473,795 Members | 2,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1314
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
1828
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 styles relating to things Python-like; see: Pythonic) this time. Warning to uninitiated: This is not real Python. I am playing make-believe. If you are a Python beginner, kindly ignore this post unless you want to get confused or you like made-up...
1
1471
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 result needs to look professional and attractive, and i need something i can get working fairly quickly with a modest learning curve. i'm looking at wxPython, because that is a pythonic solution. but i'm concerned about the scarcity of gentle...
16
2485
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 special libraries, hence you can consider it as C++). Right now, the generator program is written in C++. And I feel not so comfortable with C++. I feel C++ is an overkill. Because, I need to generate some code, hence in the program there are a...
11
5052
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 do this? Is there a better solution than putting the sequence at module scope?
4
1800
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 software in a more "Pythonic" way. My question is, should we as python developers be trying to write code that follows more of a python standard or should we try to spend our efforts to stick to a more traditional OO model? For example, in...
6
1167
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(): if __name__ == "__main__":
2
1969
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. However, when I click my snippet, nothing happens. Has anybody else experienced this? Inserting the supplied snippets works just fine. However, no snippet I create works even though it can be selected as a snippet from the intellisense.
13
2331
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 add-ons for this purpose? If not, any urls which demonstrates code snippets management?
4
2198
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 variable in it. When you changed the private variable it would also change the variables in the Property definition. But when I do it, I only get the property definition but no variables in it: public int MyProperty { get; set; }
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9043
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7541
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.