473,666 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

subtle side effect of generator/generator expression

Hi,

I initially thought that generator/generator expression is cool(sort of
like the lazy evaluation in Haskell) until I notice this side effect.
a=(x for x in range(2))
list(a) [1,2]list(a)

[]

Would this make generator/generator expression's usage pretty limited ?
As when the program/system goes beyond a single module, this behaviour
can cause subtle bugs ?

Oct 16 '05 #1
9 2017
bo****@gmail.co m wrote:
I initially thought that generator/generator expression is cool (sort of
like the lazy evaluation in Haskell) until I notice this side effect.
a=(x for x in range(2))
list(a) [1,2]list(a) []

Would this make generator/generator expression's usage pretty limited ?
nope.
As when the program/system goes beyond a single module, this behaviour
can cause subtle bugs ?


sure, in the same way as
f = open(filename)
f.read() 'hello world\n' f.read() # oops!

''

causes subtle bugs (that is, almost never)

</F>

Oct 16 '05 #2
That is exactly what I meant, in fact. These IO thing are expected to
have side effects so they are not subtle. Generator on the other hand,
is sort of "clever iteratables".

Now that I notice that, Of course I can be sure I would be careful. But
what about the following situation :

I import some function from some third party module which said, "oh, my
function returns a iteratable". Then I can only list(o) if I want to
pass it around or I consume it in only one and only one place. turning
it into a list completely negate what generator is intended
for(generate as much as you need) and using it in only one and only one
place(like your fread example) would IMO means its usage is pretty
limited.

Beside, I can do a fseek to rewind a file handle but not a generator
object.

Fredrik Lundh wrote:
As when the program/system goes beyond a single module, this behaviour
can cause subtle bugs ?


sure, in the same way as
>>> f = open(filename)
>>> f.read() 'hello world\n' >>> f.read() # oops!

''

causes subtle bugs (that is, almost never)

</F>


Oct 16 '05 #3
bo****@gmail.co m wrote:
That is exactly what I meant, in fact. These IO thing are expected to
have side effects so they are not subtle. Generator on the other hand,
is sort of "clever iteratables".

Now that I notice that, Of course I can be sure I would be careful. But
what about the following situation :

I import some function from some third party module which said, "oh, my
function returns a iteratable". Then I can only list(o) if I want to
pass it around or I consume it in only one and only one place. turning
it into a list completely negate what generator is intended
for(generate as much as you need) and using it in only one and only one
place(like your fread example) would IMO means its usage is pretty
limited.

Beside, I can do a fseek to rewind a file handle but not a generator
object.


Its a question of protocol. A iterator only guarantes that you can fetch
items from it until it is possibly exhausted. In the same way, a stream
may only guarantee you that you can read from it. If you need more, you
have tgo use a different protocol. Lists are one that allows you to
randomly access it. Files allow to seek, in addition to stream semantics.

And given that python is an imperative language with side-effects, you
don't know what sideeffects a generator can cause - which makes a
"rewind" or copy-semantics to make it work several time impossible (in
the general case, that is). So it has to focus on what it _can_ guarantee.

Haskell and other FP languages canwork differently here, as sideeffects
are made excplicit using e.g. monads or other means - which allows for
re-use of a generator-like construct. But there is no way to make this
work in pythons paradigm.

Diez
Oct 16 '05 #4
Diez B. Roggisch wrote:
Files allow to seek, in addition to stream semantics.


Some files. Not all files support seek operations. Some only support
forward seek.

</F>

Oct 16 '05 #5
If you find that you want to iterate over an iterable multiple times,
have a look at the solution that the tee() function in the itertools
module provides (http://docs.python.org/lib/itertools-functions.html).
(Have a look at the rest of the itertools module as well, for that
matter.)

Oct 16 '05 #6
True. That is why I have now reverted back to use list whenever
possible. As while list can also be modified(say in a multi-thread
situation), at least if I don't do the update(coding policy, practice
or whatever), they are sort of "guaranteed ".

I would only use generator as IO monad in Haskell, i.e. don't use it
unless I absolutely need to.

Diez B. Roggisch wrote:

Its a question of protocol. A iterator only guarantes that you can fetch
items from it until it is possibly exhausted. In the same way, a stream
may only guarantee you that you can read from it. If you need more, you
have tgo use a different protocol. Lists are one that allows you to
randomly access it. Files allow to seek, in addition to stream semantics.

And given that python is an imperative language with side-effects, you
don't know what sideeffects a generator can cause - which makes a
"rewind" or copy-semantics to make it work several time impossible (in
the general case, that is). So it has to focus on what it _can_ guarantee.

Haskell and other FP languages canwork differently here, as sideeffects
are made excplicit using e.g. monads or other means - which allows for
re-use of a generator-like construct. But there is no way to make this
work in pythons paradigm.

Diez


Oct 16 '05 #7
On Sun, 16 Oct 2005 15:52:54 +0200, Fredrik Lundh wrote:
bo****@gmail.co m wrote:
I initially thought that generator/generator expression is cool (sort of
like the lazy evaluation in Haskell) until I notice this side effect.
>>>a=(x for x in range(2))
>>>list(a)

[1,2]
>>>list(a)

[]

Would this make generator/generator expression's usage pretty limited ?


nope.


In fairness, it is a pretty big Gotcha.
As when the program/system goes beyond a single module, this behaviour
can cause subtle bugs ?


sure, in the same way as
>>> f = open(filename)
>>> f.read() 'hello world\n' >>> f.read() # oops!

''

causes subtle bugs (that is, almost never)


Are you saying that the bugs it causes aren't subtle? *wink*
--
Steven

Oct 16 '05 #8
thanks. I was looking for scanl in itertools but can't find it so I
implement my own then run into some subtle bugs which first made me
think my scanl is the problem. Then notice my wrong perception about
generator(and iterable in general, though the built-in iterables like
list, dict don't seem to have this characteristic) .

Simon Percivall wrote:
If you find that you want to iterate over an iterable multiple times,
have a look at the solution that the tee() function in the itertools
module provides (http://docs.python.org/lib/itertools-functions.html).
(Have a look at the rest of the itertools module as well, for that
matter.)


Oct 16 '05 #9
> Are you saying that the bugs it causes aren't subtle? *wink*

Exactly. Destructive generator problems are caught almost immediately.

Oct 16 '05 #10

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

Similar topics

24
3342
by: Mahesh Padmanabhan | last post by:
Hi, When list comprehension was added to the language, I had a lot of trouble understanding it but now that I am familiar with it, I am not sure how I programmed in Python without it. Now I see that generator expressions have been added to the language with 2.4 and I question the need for it. I know that it allows for lazy evaluation which speeds things up for larger lists but why was it necessary to add it instead of improving list...
16
2281
by: Peter Otten | last post by:
To confuse a newbies and old hands alike, Bengt Richter wrote: > Need something more straightforward, e.g., a wrapped one-liner: > > >>> def guess(n=3): print ("You're right!", 'No more tries for > >>> you!!!') > ... (x for x in xrange(n) for t in > ... if not t or iter().next())] > ... > >>> guess()
3
2384
by: Sensorflo | last post by:
After browsing though many newsgroups articels I'm still not shure how operator precedence, operator associativity, sequence points, side effects go together. Currently I have the following view: An expression a = b() + c() * d++; can be transformed with the rules of operator associativity and operator precedence into a tree
5
3221
by: Niklaus | last post by:
This is one of the posts that i got. ------------------------------ A "side effect" of an operation is something that *happens*, not something that *is produced*. Examples: In the expression 2+2, the value 4 *is produced*. Nothing *happens*. Thus, 4 is the value of the expression, and it has no side effects. In the expression g=2.0, the value 2.0 is produced. What *happens* is that 2.0 is assigned to g. Thus, 2.0 is the value of the...
17
2338
by: dingoatemydonut | last post by:
The C99 standard states: "In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object)." Does that mean that in the following code, *p does not have to be evaluated since its side...
0
968
by: Johannes Nix | last post by:
There is a subtle bug in the RandomArray module, which is part of the Numeric package. It causes the random number generator RandomArray.normal() to return incorrect values when invoked on Linux compiled with gcc on a AMD Opteron machine, that is, a system with 64-Bit CPU and ILP64 data model. The result will depend on the data model and the aligning of the C implementation on the used architecture. The bug seems not to be triggered on...
4
1540
by: andrew browning | last post by:
does i++ have the same potential for side effects as its pre-fix equivilent ++i? and if so, is it always dangerous to use them, as in a for loop, or only in certain circumstances?
7
8107
by: Laurent Pointal | last post by:
on win32] Given the following: 45 .... (<generator object at 0x00A79788>,) .... File "<stdin>", line 1 SyntaxError: invalid syntax
4
3024
by: Academia | last post by:
I get the following watch message: tsSource.Text.ToUpper() This expression causes side effects and will not be evaluated string The Text is &Edit
0
8871
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...
0
8781
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8640
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
7386
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
6198
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
4198
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...
1
2771
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
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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.