473,586 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extend for loop syntax with if expr like listcomp&genexp ?


E.g., so we could write

for x in seq if x is not None:
print repr(x), "isn't None ;-)"

instead of

for x in (x for x in seq if x is not None):
print repr(x), "isn't None ;-)"

just a thought.

Regards,
Bengt Richter
Jul 21 '05 #1
6 1506
Bengt Richter wrote:
E.g., so we could write

for x in seq if x is not None:
Chundrous; looks like that p**l language ...
print repr(x), "isn't None ;-)"

instead of

for x in (x for x in seq if x is not None):
Byzantine ...
print repr(x), "isn't None ;-)"

just a thought.


What's wrong with the following?

for x in seq:
if x is not None:
print repr(x), "isn't None ;-)"
Jul 21 '05 #2
Bengt Richter wrote:
E.g., so we could write

for x in seq if x is not None:
print repr(x), "isn't None ;-)"

instead of

for x in (x for x in seq if x is not None):
print repr(x), "isn't None ;-)"

just a thought.

Regards,
Bengt Richter


Is it new idea month? :)

That would seem to follow the pattern of combining sequential lines that
end in ':'.
if pay<10 if hours>10 if stressed:
sys.exit()

That would be the same as using ands.

And this gives us an if-try pattern with a shared else clause.

if trapped try:
exit = find('door')
except:
yell_for_help()
else: #works for both if and try! ;-D
leave()
Which would be the same as:

if trapped:
try:
exit = find('door')
except:
yell_for_help()
else:
leave()
else:
leave()
Interesting idea, but I think it might make reading other peoples code
more difficult.
Cheers,
Ron
Jul 21 '05 #3
On Tue, 12 Jul 2005 10:12:33 +1000, John Machin <sj******@lexic on.net> wrote:
Bengt Richter wrote:
E.g., so we could write

for x in seq if x is not None:
Chundrous; looks like that p**l language ...

^^^^^^^^^--piqued my interest, where'd that come from? ;-)
print repr(x), "isn't None ;-)"

instead of

for x in (x for x in seq if x is not None):
Byzantine ...

Perhaps not if you wanted to enumerate the selected elements, as in
for i, x in enumerate(x for x in seq if x is not None):
print repr(x), "isn't None ;-)"

just a thought.


What's wrong with the following?

for x in seq:
if x is not None:
print repr(x), "isn't None ;-)"


Nothing. Just noting that there's (at least) two kinds of for --
the plain old one, and the ones inside list comprehensions and generator
expressions, and it struck me that not allowing the full listcomp/genexp
syntax in the ordinary for context was a seemingly unnecessary restriction.

Regards,
Bengt Richter
Jul 21 '05 #4
On Monday 11 July 2005 08:53 pm, Bengt Richter wrote:
On Tue, 12 Jul 2005 10:12:33 +1000, John Machin <sj******@lexic on.net> wrote:
Bengt Richter wrote:
for x in (x for x in seq if x is not None):

Byzantine ...

Perhaps not if you wanted to enumerate the selected elements, as in
for i, x in enumerate(x for x in seq if x is not None):


Seems like a bug waiting to happen -- wouldn't someone using that
idiom most likely have *meant* something like this:

for i,x in enumerate(seq):
if x is not None:
print "seq[%d] = %s is not None" % (i, repr(x))

?

But of course that's not equivalent. It's hard to imagine a
use case for an enumerated loop when the object being
iterated over is anonymous (will be lost as soon as the loop
exits).

--
Terry Hancock ( hancock at anansispacework s.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 21 '05 #5
On Tue, 12 Jul 2005 23:07:07 -0500, Terry Hancock <ha*****@anansi spaceworks.com> wrote:
On Monday 11 July 2005 08:53 pm, Bengt Richter wrote:
On Tue, 12 Jul 2005 10:12:33 +1000, John Machin <sj******@lexic on.net> wrote:
>Bengt Richter wrote:
>> for x in (x for x in seq if x is not None):
>Byzantine ...

Perhaps not if you wanted to enumerate the selected elements, as in
for i, x in enumerate(x for x in seq if x is not None):


Seems like a bug waiting to happen -- wouldn't someone using that
idiom most likely have *meant* something like this:

for i,x in enumerate(seq):
if x is not None:
print "seq[%d] = %s is not None" % (i, repr(x))

?

But of course that's not equivalent. It's hard to imagine a
use case for an enumerated loop when the object being
iterated over is anonymous (will be lost as soon as the loop
exits).

Line numbers in a listing of non-None things?
Page breaks at the right places?
Filtering out '' instead of NOne from results of a string split before creating numbered
html names for links to non-blank text elements in rendering text as html?
I dunno, seems like at least a few possibilities for something halfway sensible...

Regards,
Bengt Richter
Jul 21 '05 #6
Terry Hancock <ha*****@anansi spaceworks.com> writes:
But of course that's not equivalent. It's hard to imagine a
use case for an enumerated loop when the object being
iterated over is anonymous (will be lost as soon as the loop exits).


Huh? Not at all.

print 'List of Python fans:'
for i,x in enumerate([p for p in people if p.favorite_lang uage == 'Python']):
print '%d. %s'% (i, x.name)
Jul 21 '05 #7

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

Similar topics

16
2595
by: George Sakkis | last post by:
I'm sure there must have been a past thread about this topic but I don't know how to find it: How about extending the "for <X> in" syntax so that X can include default arguments ? This would be very useful for list/generator comprehensions, for example being able to write something like: instead of the less elegant explicit loop version...
19
2952
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $ Last-Modified: $Date: 2003/09/22 04:51:49 $ Author: Nicolas Fleury <nidoizo at gmail.com> Status: Draft Type: Standards Track
8
13970
by: Jon Maz | last post by:
Hi All, Quick one: several times in vb.net code I have got off the net there are lines such as: For i As Integer = 0 To 10 which, on compiling with VS2002, produce the following error: error BC30451: Name 'i' is not declared.
2
10650
by: Adrienne Boswell | last post by:
Today, I am braindead... I can't seem to find this in Google, and I don't remember where I used this code before (so I can't steal it from myself). Anyway, all I want to do is: for i = 0 to 20 step -1 response.write "<br>" & i next For some reason, that doesn't work. It shows nothing. I know I am missing something very simple, but,...
11
25507
by: Rene | last post by:
Quick question, what is the point for forcing the semicolon at the end of the while statement? See example below: x = 0; do { x = x + 1; }while (x < 3); What's the point of having the semicolon after the (x < 3)? Why can't the
9
2739
by: Ron Wan | last post by:
I am inexperienced at this (posting and writitng code). I am using an Access2000 datasheet form with checkboxes to select PDF documents for printing. The PDFs are given filenames based on the doc_id assigned as the primary key in the doc table when the doc is entered into the database. What I can't do is use a form to select which docs to print...
0
2055
by: Mark Dickinson | last post by:
On Nov 19, 5:48 pm, Johannes Bauer <dfnsonfsdu...@gmx.dewrote: Python 2.6 has itertools.product: http://docs.python.org/library/itertools.html#itertools.product If you don't have Python 2.6 available, then the documentation above also contains (almost) equivalent Python code that might work for you.
2
1433
ddtpmyra
by: ddtpmyra | last post by:
I have check box on my page which has its equivalent value. How do I insert the value inside my tables in single row for each check box using LOOP ex. <input type="checkbox" name="chk1" value="Y" <?php if $value=='Karen Lincoln'){ echo "checked=\"true\""; }?>>CAT <input type="checkbox" name="chk2" value="Y" <?php if($value=='Karen...
0
7911
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...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7954
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...
0
8215
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...
0
3836
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...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
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...

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.