473,769 Members | 4,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Better writing in python

I'm just wondering, if I could write a in a "better" way this code

lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.appe nd(arg)
else:
lOptional.appen d(arg)
return (lMandatory, lOptional)

I think there is a better way, but I can't see how...

Oct 24 '07
19 1871
On Oct 24, 8:02 am, kyoso...@gmail. com wrote:
On Oct 24, 7:09 am, Alexandre Badez <alexandre.ba.. .@gmail.comwrot e:
I'm just wondering, if I could write a in a "better" way this code
lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.appe nd(arg)
else:
lOptional.appen d(arg)
return (lMandatory, lOptional)
I think there is a better way, but I can't see how...

You might look into list comprehensions. You could probably do this
with two of them:

<code>
# completely untested
lMandatory = [arg for arg in cls.dArguments if arg is True]
lOptional = [arg for arg in cls.dArguments if arg is False]
</code>

Something like that. I'm not the best with list comprehensions, so I
may have the syntax just slightly off. See the following links for
more information:

http://www.secnetix.de/olli/Python/l...tut/node7.html

Mike
After reading the others replies, it makes me think that I'm barking
up the wrong tree. Ah well.

Mike

Oct 24 '07 #11
On 2007-10-24, Alexandre Badez <al************ *@gmail.comwrot e:
I'm just wondering, if I could write a in a "better" way this
code

lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.appe nd(arg)
else:
lOptional.appen d(arg)
return (lMandatory, lOptional)

I think there is a better way, but I can't see how...
You can do it shorter, not sure that it also qualifies as better....

d = { True : [] , False : [] }
for arg in cls.arguments:
d[arg == True].append(arg)

return d[True], d[False]

One potential problem here is that 'arg == True' may not be the same as 'if
arg:'. I couldn't come up with a better equivalent expression, maybe one of the
other readers knows more about this?
Albert

Oct 24 '07 #12
Alexandre Badez wrote:
I'm just wondering, if I could write a in a "better" way this code
[...]
I think there is a better way, but I can't see how...
What's "better" for you? Shorter? More performant? More readable?
Complying with best practice? Closely following a specific
programming paradigm?

Regards,
Björn

--
BOFH excuse #403:

Sysadmin didn't hear pager go off due to loud music from bar-room
speakers.

Oct 24 '07 #13
On Oct 24, 2:02 pm, kyoso...@gmail. com wrote:
On Oct 24, 7:09 am, Alexandre Badez <alexandre.ba.. .@gmail.comwrot e:
I'm just wondering, if I could write a in a "better" way this code
lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.appe nd(arg)
else:
lOptional.appen d(arg)
return (lMandatory, lOptional)
I think there is a better way, but I can't see how...

You might look into list comprehensions. You could probably do this
with two of them:

<code>
# completely untested
lMandatory = [arg for arg in cls.dArguments if arg is True]
lOptional = [arg for arg in cls.dArguments if arg is False]
</code>

Something like that. I'm not the best with list comprehensions, so I
may have the syntax just slightly off.
Your list comprehensions are right, but 'arg is True' and 'arg is
False' are better written as 'arg' and 'not arg' respectively.

--
Paul Hankin

Oct 24 '07 #14
On Oct 24, 4:15 pm, Paul Hankin <paul.han...@gm ail.comwrote:
On Oct 24, 2:02 pm, kyoso...@gmail. com wrote:
On Oct 24, 7:09 am, Alexandre Badez <alexandre.ba.. .@gmail.comwrot e:
I'm just wondering, if I could write a in a "better" way this code
lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.appe nd(arg)
else:
lOptional.appen d(arg)
return (lMandatory, lOptional)
I think there is a better way, but I can't see how...
You might look into list comprehensions. You could probably do this
with two of them:
<code>
# completely untested
lMandatory = [arg for arg in cls.dArguments if arg is True]
lOptional = [arg for arg in cls.dArguments if arg is False]
</code>
Something like that. I'm not the best with list comprehensions, so I
may have the syntax just slightly off.

Your list comprehensions are right, but 'arg is True' and 'arg is
False' are better written as 'arg' and 'not arg' respectively.

--
Paul Hankin
Anyone know why towards arg is True and arg is False, arg is None is
faster than arg == None ...

Oct 24 '07 #15
On Oct 24, 10:42 am, cokofree...@gma il.com wrote:
On Oct 24, 4:15 pm, Paul Hankin <paul.han...@gm ail.comwrote:
On Oct 24, 2:02 pm, kyoso...@gmail. com wrote:
On Oct 24, 7:09 am, Alexandre Badez <alexandre.ba.. .@gmail.comwrot e:
I'm just wondering, if I could write a in a "better" way this code
lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.appe nd(arg)
else:
lOptional.appen d(arg)
return (lMandatory, lOptional)
I think there is a better way, but I can't see how...
You might look into list comprehensions. You could probably do this
with two of them:
<code>
# completely untested
lMandatory = [arg for arg in cls.dArguments if arg is True]
lOptional = [arg for arg in cls.dArguments if arg is False]
</code>
Something like that. I'm not the best with list comprehensions, so I
may have the syntax just slightly off.
Your list comprehensions are right, but 'arg is True' and 'arg is
False' are better written as 'arg' and 'not arg' respectively.
--
Paul Hankin

Anyone know why towards arg is True and arg is False, arg is None is
faster than arg == None ...
And quite often incorrect, especially the "arg is True" and "arg is
False".

Oct 24 '07 #16
On Wed, 24 Oct 2007 16:04:28 +0200, A.T.Hofkamp wrote:
>On 2007-10-24, Alexandre Badez <al************ *@gmail.comwrot e:
I'm just wondering, if I could write a in a "better" way this
code

lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.appe nd(arg)
else:
lOptional.appen d(arg)
return (lMandatory, lOptional)

I think there is a better way, but I can't see how...

You can do it shorter, not sure that it also qualifies as better....

d = { True : [] , False : [] }
for arg in cls.arguments:
d[arg == True].append(arg)

return d[True], d[False]

One potential problem here is that 'arg == True' may not be the same as 'if
arg:'. I couldn't come up with a better equivalent expression, maybe one of the
other readers knows more about this?
With ``if arg:`` the interpreter asks `arg` for its "boolean value". So a
better way would be:

d[bool(arg)].append(arg)

As `True` and `False` are instances of `int` with the values 1 and 0 it's
possible to replace the dictionary by a list:

tmp = [[], []]
for arg in cls.arguments:
tmp[bool(arg)].append(arg)
return tmp[1], tmp[0]

Maybe that's nicer. Maybe not.

Ciao,
Marc 'BlackJack' Rintsch
Oct 24 '07 #17
Bjoern Schliessmann a écrit :
Alexandre Badez wrote:
>I'm just wondering, if I could write a in a "better" way this code
[...]
I think there is a better way, but I can't see how...

What's "better" for you? Shorter? More performant? More readable?
Complying with best practice? Closely following a specific
programming paradigm?
I think the OP meant "more pythonic".

Oct 24 '07 #18
On Oct 24, 9:04 am, "A.T.Hofkam p" <h...@se-162.se.wtb.tue. nlwrote:
On 2007-10-24, Alexandre Badez <alexandre.ba.. .@gmail.comwrot e:
I'm just wondering, if I could write a in a "better" way this
code
lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.appe nd(arg)
else:
lOptional.appen d(arg)
return (lMandatory, lOptional)
I think there is a better way, but I can't see how...

You can do it shorter, not sure that it also qualifies as better....

d = { True : [] , False : [] }
for arg in cls.arguments:
d[arg == True].append(arg)

return d[True], d[False]

One potential problem here is that 'arg == True' may not be the same as 'if
arg:'. I couldn't come up with a better equivalent expression, maybe one of the
other readers knows more about this?

Albert
d[bool(arg)].append(arg) resolves your concern?

Oct 24 '07 #19
On Oct 24, 3:46 pm, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
For a 'python like' look lose the Hungarian notation (even Microsoft
have largely stopped using it)
I wish I could.
But my corporation do not want to apply python.org coding rules
increase the indentation to 4 spaces,
Well, it is in my python file.
I do not do it here, because I'm a bit lazy.
and also get rid of the spurious parentheses around the result.
Thanks
Otherwise it is fine: clear and to the point.
Thanks
>
If you really wanted you could write something like:

m, o = [], []
for arg in cls.dArguments:
(m if cls.dArguments[arg]['mandatory'] else o).append(arg)
return m, o

Or even:
m, o = [], []
action = [o.append, m.append]
for arg in cls.dArguments:
action[bool(cls.dArgum ents[arg]['mandatory'])](arg)
return m, o

but it just makes the code less clear, so why bother?
And finally thanks again ;)

Oct 24 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2143
by: Bengt Richter | last post by:
>>> hex(-5) __main__:1: FutureWarning: hex()/oct() of negative int will return a signed string in Python 2.4 and up '0xfffffffb' >>> hex(-5) '0xfffffffb' >>> hex(-5L) '-0x5L' That is sooo ugly. I suppose it is for a backwards compatible repr, but couldn't we
220
19156
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
6
1672
by: Michael | last post by:
Hi, I'm fairly new at Python, and have the following code that works but isn't very concise, is there a better way of writing it?? It seems much more lengthy than python code i have read..... :-) (takes a C++ block and extracts the namespaces from it) def ExtractNamespaces(data): print("Extracting Namespaces")
2
3791
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short- cut solution for finding collisions in SHA-1 . Their result
6
1455
by: Dan Jacobson | last post by:
Can I feel even better about using perl vs. python, as apparently python's dependence of formatting, indentation, etc. vs. perl's "(){};" etc. makes writing python programs perhaps very device dependent. Whereas perl can be written on a tiny tiny screen, and can withstand all kinds of users with various disabilities, etc.? Also perl is easier to squeeze into makefiles.
6
1453
by: Anton Vredegoor | last post by:
Since a few days I've been experimenting with a construct that enables me to send the sourcecode of the web page I'm reading through a Python script and then into a new tab in Mozilla. The new tab is automatically opened so the process feels very natural, although there's a lot of reading, filtering and writing behind the scene. I want to do three things with this post: A) Explain the process so that people can try it for themselves...
7
1803
by: apollonius2 | last post by:
Greetings, I have been working on a little project today to help me better understand classes in Python (I really like Python). I am a self taught programmer and consider myself to fall in the "beginner" category for sure. It was initially sparked by reading about "state machines". This was my attempt at it however I feel it is not quite where it should be: ON = "ON"
3
1537
by: koutoo | last post by:
I have a code that writes to 2 seperate files. I keep getting a "list index out of range" error. The strange part is that when checking the files that I'm writing too, the script has already iterated through and finished writing, yet the error stated implies that it hasn't? So how can it be, that my script has written to the files, yet the error is stating that it hasn't made it through the script? I'll have 15 files that I have...
13
1178
by: Marco Bizzarri | last post by:
Sorry... pressed enter but really didn't want to. As I said, let's say I have a class class A: def __init__(self): self.x = None
0
9589
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
9423
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
10219
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
9998
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
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
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
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.