473,385 Members | 2,069 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,385 software developers and data experts.

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 1496
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******@lexicon.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******@lexicon.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 anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 21 '05 #5
On Tue, 12 Jul 2005 23:07:07 -0500, Terry Hancock <ha*****@anansispaceworks.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******@lexicon.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*****@anansispaceworks.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_language == '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
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...
19
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 $...
8
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: ...
2
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...
11
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...
9
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...
0
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...
2
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"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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...
0
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...

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.