Mailing List Archive

Style/efficiency question using 'in'
If someone could spare a mo to clarify -

If I do something like :-

------------------
def func1():
return 1,2,3,4,5,6,7,8

for x in func1():
print x
------------------

it works as expected but is the use of a function in a loop undesirable?
Is the function called once to build the loop or is it called each loop
increment and therefore undesirable if there is much overhead?

greatful for any comments

cheers
Nick/Oxford
Style/efficiency question using 'in' [ In reply to ]
In article <3720B3B9.98BED320@earth.ox.ac.uk>, Nick Belshaw
<nickb@earth.ox.ac.uk> wrote:

> If I do something like :-
>
> ------------------
> def func1():
> return 1,2,3,4,5,6,7,8
>
> for x in func1():
> print x
> ------------------
>
> it works as expected but is the use of a function in a loop undesirable?
> Is the function called once to build the loop or is it called each loop
> increment and therefore undesirable if there is much overhead?

It's called once to build the loop, as you can prove to yourself by
inserting "print 'spam'" into func1().

Cheers,
-- Joe

--
,------------------------------------------------------------------.
| Joseph J. Strout Biocomputing -- The Salk Institute |
| joe@strout.net http://www.strout.net |
`------------------------------------------------------------------'
Check out the Mac Web Directory! http://www.strout.net/macweb.cgi
Style/efficiency question using 'in' [ In reply to ]
Pythonistas--

Nick Belshaw wrote:
>
> If someone could spare a mo to clarify -
>
> If I do something like :-
>
> ------------------
> def func1():
> return 1,2,3,4,5,6,7,8
>
> for x in func1():
> print x
> ------------------
>
> it works as expected but is the use of a function in a loop undesirable?
> Is the function called once to build the loop or is it called each loop
> increment and therefore undesirable if there is much overhead?
>

l = (1,2,3,4,5,6,7,8)
for x in l:
print x

or

for x in 1,2,3,4,5,6,7,8:
print x

And your version, are all the same. In each case, a tuple is created;
in the middle case, the tuple is named. There is no ``loop
increment''--for just steps through the items in the sequence that it
sees in the order that it sees them. That's why handing for a reversed
list doesn't produce unexpected behaviour.

The only undesirable effects of these three methods are that in (1), you
have created a named function that may only be used once; in (2), I
created a named variable which may only be used once; and in (3), the
tuple has no name and ceases to exist at the end of the for loop. Of
course, that means you can't use the tuple again, either;-)

<python-aint-C-C?>-ly y'rs,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan@callware.com
http://www.pauahtun.org
See also:
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
----------------------------------------------
Style/efficiency question using 'in' [ In reply to ]
In article <3720B3B9.98BED320@earth.ox.ac.uk>,
Nick Belshaw <nickb@earth.ox.ac.uk> wrote:
> If someone could spare a mo to clarify -
>
> If I do something like :-
>
> ------------------
> def func1():
> return 1,2,3,4,5,6,7,8
>
> for x in func1():
> print x
> ------------------

This may not be appropriate to the problem that you are really trying to
solve, but are you aware of this (range(n) will make your list for you):

>>> range(8)
[0, 1, 2, 3, 4, 5, 6, 7]
>>> for x in range(8):
... print x+1
...
1
2
3
4
5
6
7
8
>>>

----
David Moore

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
Style/efficiency question using 'in' [ In reply to ]
On Fri, 23 Apr 1999, Ivan Van Laningham wrote several variations
on 'for x in y' :


And just to confuse the new user even further, lets add pseudo
sequences onto the pile.

The actual protocol that 'for' uses is to get the next item in
the sequence until an IndexError is raised. Earlier versions
of Python compared the index to len() each time thru. I think
I can claim some credit in inspiring Guido to change that behaviour
by writing some Really Ugly classes which lied about their length
in order to implement indefinite or lazy sequences. Now, it's a
snap: all you have to do is implement __getitem__ to make something
appear as a sequence to 'for' and 'in'.

For example, rather than reading in a entire file and splitting it
into tokens or chunks all at once, you can make a lazy sequence which
reads another chunk of the file if needed and parses the next token
each time __getitem__ is called with a larger index. You main loop
would be something like 'for tok in Tokens( filename ): '

A simpler example:

>>> class Squares:
... def __init__( self, n ):
... self.n = n
... def __getitem__( self, i ):
... if i < self.n : return i*i
... else: raise IndexError
...
>>> sq = Squares(10)
>>> for x in sq: print x
...
0
1
4
9
16
25
36
49
64
81
>>> if 4 in sq : print 'YES'
...
YES
>>>

---| Steven D. Majewski (804-982-0831) <sdm7g@Virginia.EDU> |---
---| Department of Molecular Physiology and Biological Physics |---
---| University of Virginia Health Sciences Center |---
---| P.O. Box 10011 Charlottesville, VA 22906-0011 |---

Caldera Open Linux: "Powerful and easy to use!" -- Microsoft(*)
(*) <http://www.pathfinder.com/fortune/1999/03/01/mic.html>