473,511 Members | 15,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

My Generator Paradox!

I am afraid that this is the first time in which I would probably need
something explained to me as if I were a little child. I am having a
hard time getting this through my thick skull. What in the world is
wrong with this!?

''' ################################################## ######### '''

def generatorFunction(sequence=['item1', 'item2', 'item3']):
for item in sequence:
yield item

yieldedValue = generatorFunction()

'''this seems to work perfectly.'''
print '-' * 32
print yieldedValue # <generator object at 0xb723014c>
print yieldedValue.next() # item1
print yieldedValue.next() # item2
print yieldedValue.next() # item3

'''this is where things don't make any sense!'''
print '-' * 32
print generatorFunction() # <generator object at 0xb723022c>
print generatorFunction().next() # item1
print generatorFunction().next() # item1
print generatorFunction().next() # item1

''' ################################################## ######### '''

the first set of calls assigned to yieldedValue work but the second set
without assignment don't. I asked for help on this at #python (I love
those people in there!) and was told the following...
generatorFunction() is a call (obvious) when calling the second set, I
am resetting the iteration and this explains why I only and always get
item1.

ok. *but* why in the world does the first set of calls work?
technically, isn't yieldedValue == generatorFunction() on a name basis?
I mean isn't the following technically the same?

generatorFunction()
yieldedValue = generatorFunction()

aren't they both the same? To me they should be but obviously this
creates the point of this paradox. I don't understand what is happening
here... Can someone care to explain why the assignment works but not
the direct call? In a sense shouldn't the assignment yield the same
results as the direct call and vice versa? I am confused :(

Thank you for any help on this!

Mar 17 '06 #1
11 1718
Em Qui, 2006-03-16 Ã*s 16:17 -0800, vbgunz escreveu:
print generatorFunction() # <generator object at 0xb723022c>
print generatorFunction().next() # item1
print generatorFunction().next() # item1
print generatorFunction().next() # item1


Each time you say "generatorFunction()", it gives you a new generator,
thus returning the first item again.

Mar 17 '06 #2
vbgunz wrote:
def generatorFunction(sequence=['item1', 'item2', 'item3']):
for item in sequence:
yield item

yieldedValue = generatorFunction()
You're creating an iterator here and binding it to name yieldedValue
(which is bogus, it should be named valueGenerator or sth like that).
'''this seems to work perfectly.'''
print '-' * 32
print yieldedValue # <generator object at 0xb723014c>
print yieldedValue.next() # item1
print yieldedValue.next() # item2
print yieldedValue.next() # item3
You're calling your iterator's next() method getting all of values, as
expected.
'''this is where things don't make any sense!'''
print '-' * 32
print generatorFunction() # <generator object at 0xb723022c> You're creating a new iterator here.
print generatorFunction().next() # item1 Another anonymous iterator gets created here. Instantly its next()
method is called, yielding first value.
print generatorFunction().next() # item1 And so on...
generatorFunction() is a call (obvious) when calling the second set, I
am resetting the iteration and this explains why I only and always get
item1.

ok. *but* why in the world does the first set of calls work?
technically, isn't yieldedValue == generatorFunction() on a name
basis? I mean isn't the following technically the same?

generatorFunction()
yieldedValue = generatorFunction()


Well, first statement creates new iterator which is garbage collected
right away (as it has no bindings). Second statement creates an
iterator and binds it to name yieldedValue. Then it can be used as
typical iterator. Calling yieldedValue.next() just calls method of the
same iterator you've created a moment ago. It's still the same object.
The difference is like the difference between following two lines:

list() # creating a new list
new_list = list() # creating a new list and binding its name

So, rewriting your example from generator to dictionary objects:

alist = [1, 2, 3, 4, 5]
print alist # [1, 2, 3, 4, 5]
print alist.pop() # 5
print alist.pop() # 4
print alist.pop() # 3

print [1, 2, 3, 4, 5] # [1, 2, 3, 4, 5]
print [1, 2, 3, 4, 5].pop() # 5
print [1, 2, 3, 4, 5].pop() # 5
print [1, 2, 3, 4, 5].pop() # 5

Remember that generator is an object and you'll be fine.

mk
--
. o . >> http://joker.linuxstuff.pl <<
. . o It's easier to get forgiveness for being wrong
o o o than forgiveness for being right.
Mar 17 '06 #3
vbgunz wrote:
I am afraid that this is the first time in which I would probably need
something explained to me as if I were a little child. I am having a
hard time getting this through my thick skull. What in the world is
wrong with this!?

''' ################################################## ######### '''

def generatorFunction(sequence=['item1', 'item2', 'item3']):
for item in sequence:
yield item

yieldedValue = generatorFunction()

'''this seems to work perfectly.'''
print '-' * 32
print yieldedValue # <generator object at 0xb723014c>
print yieldedValue.next() # item1
print yieldedValue.next() # item2
print yieldedValue.next() # item3

'''this is where things don't make any sense!'''
print '-' * 32
print generatorFunction() # <generator object at 0xb723022c>
print generatorFunction().next() # item1
print generatorFunction().next() # item1
print generatorFunction().next() # item1

''' ################################################## ######### '''

the first set of calls assigned to yieldedValue work but the second set
without assignment don't. I asked for help on this at #python (I love
those people in there!) and was told the following...
generatorFunction() is a call (obvious) when calling the second set, I
am resetting the iteration and this explains why I only and always get
item1.

ok. *but* why in the world does the first set of calls work?
technically, isn't yieldedValue == generatorFunction() on a name basis?
I mean isn't the following technically the same?

generatorFunction()
yieldedValue = generatorFunction()

aren't they both the same?

[snip]

In that short example, they happen to be different, but equivalent,
objects, but that will not always be the case. Consider this:
a = generatorFunction()
b = generatorFunction()
print a.next()

item1

Now a and b are both generators for generatorFunction, but calling
a.next() again will return 'item2', while calling b.next() will return
'item1'. The value returned by generatorFunction is an object, which
has an internal state that makes it distinct from other objects of the
same type. So once your yieldedValue has been altered by calling the
next() method, it is no longer equivalent to a fresh instance of the
generator.

-- David

Mar 17 '06 #4
it's easy to explain

class X:
pass

x=X()
y=X()

x and y are different instances
one can put in x
x.item = 1
y doesn't even have an attribute item for example

similar with generators
they are *different* objects of same kind generator
def fib(): .... a,b = 1,1
.... while True:
.... a,b = b,a+b
.... yield a,b
.... f1 = fib()
f2 = fib()
f1 <generator object at 0x4042866c> f2 <generator object at 0x404db42c> # different addresses f1 is f2 False f1.next() (1, 2) f1.next() (2, 3) f1.next() (3, 5)

f2.next() (1, 2)


it's only natural that each objects starts it's own fibonaci serie

hth, Daniel

Mar 17 '06 #5

vbgunz wrote:
I am afraid that this is the first time in which I would probably need
something explained to me as if I were a little child. I am having a
hard time getting this through my thick skull. What in the world is
wrong with this!?

''' ################################################## ######### '''

def generatorFunction(sequence=['item1', 'item2', 'item3']):
for item in sequence:
yield item

yieldedValue = generatorFunction()

'''this seems to work perfectly.'''
print '-' * 32
print yieldedValue # <generator object at 0xb723014c>
print yieldedValue.next() # item1
print yieldedValue.next() # item2
print yieldedValue.next() # item3

'''this is where things don't make any sense!'''
print '-' * 32
print generatorFunction() # <generator object at 0xb723022c>
print generatorFunction().next() # item1
print generatorFunction().next() # item1
print generatorFunction().next() # item1

''' ################################################## ######### '''

the first set of calls assigned to yieldedValue work but the second set
without assignment don't. I asked for help on this at #python (I love
those people in there!) and was told the following...
generatorFunction() is a call (obvious) when calling the second set, I
am resetting the iteration and this explains why I only and always get
item1.

ok. *but* why in the world does the first set of calls work?
technically, isn't yieldedValue == generatorFunction() on a name basis?
I mean isn't the following technically the same?

generatorFunction()
yieldedValue = generatorFunction()
No. Look at this
a = generatorFunction()
b = generatorFunction()
a==b False

Why aren'y they the same? Here's a clue:
generatorFunction() <generator object at 0x00B28418> generatorFunction() <generator object at 0x00AD24E0>

Note the addresses are different.

Try this
b.next() 'item1' b.next() 'item2' generatorFunction().next() 'item1'

Just like your example, the generator re-initailized and
printed item1. But that's not the same generator as b
b.next()

'item3'


aren't they both the same? To me they should be but obviously this
creates the point of this paradox. I don't understand what is happening
here... Can someone care to explain why the assignment works but not
the direct call? In a sense shouldn't the assignment yield the same
results as the direct call and vice versa? I am confused :(

Thank you for any help on this!


Mar 17 '06 #6
"vbgunz" wrote:
I am afraid that this is the first time in which I would probably need
something explained to me as if I were a little child. I am having a
hard time getting this through my thick skull. What in the world is
wrong with this!?

''' ################################################## ######### '''

def generatorFunction(sequence=['item1', 'item2', 'item3']):
for item in sequence:
yield item

yieldedValue = generatorFunction()

'''this seems to work perfectly.'''
print '-' * 32
print yieldedValue # <generator object at 0xb723014c>
print yieldedValue.next() # item1
print yieldedValue.next() # item2
print yieldedValue.next() # item3

'''this is where things don't make any sense!'''
print '-' * 32
print generatorFunction() # <generator object at 0xb723022c>
print generatorFunction().next() # item1
print generatorFunction().next() # item1
print generatorFunction().next() # item1

''' ################################################## ######### '''


does the following surprise you too ?

f = open("filename")
print f.readline() # prints first line
print f.readline() # prints second line
print f.readline() # prints third line

print open("filename").readline() # prints first line
print open("filename").readline() # prints first line
print open("filename").readline() # prints first line

</F>

Mar 17 '06 #7
I believe I understand now. the yield keyword is sort of like a cousin
to return. return will bring back an object I can work with and so does
yield *but* yield's object will most likely support the .next() method.

So, if I worked with a function that ends with the return keyword and
it returns a list, I can run list operations and list methods on it. if
a function ends with the yield keyword a generator should return.

So, calling the function by it's name will always reset and initialize
the generator. Whereas assigning to the functions yielded return grants
access to the real generator in which I can use the next() method.

Maybe I've explained it wrong *but* it does make sense to me now. I
just couldn't grasp it because I am still new to the keyword yield and
didn't know it sort of works like return.

I really wish to thank you fellas so much for your examples and
explanations! I think I got it! I thank you all again!

Mar 17 '06 #8
vbgunz wrote:
I believe I understand now. the yield keyword is sort of like a cousin
to return. return will bring back an object I can work with and so does
yield *but* yield's object will most likely support the .next() method.


No, that's not really how it works. When a generator function is called, it
returns the generator object immediately. None of the code inside is executed.
Every time you call that generator function, you get a new generator object with
the initial state. The objects that are yielded inside the code don't show up yet.

The code inside the generator gets executed only when the generator object is
iterated over (or its .next() method is called). The objects that are yielded
are the results of calling the .next() method.

--
Robert Kern
ro*********@gmail.com

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Mar 17 '06 #9
Robert Kern wrote:
vbgunz wrote:
I believe I understand now. the yield keyword is sort of like a cousin
to return. return will bring back an object I can work with and so does
yield *but* yield's object will most likely support the .next() method.


No, that's not really how it works. When a generator function is called, it
returns the generator object immediately. None of the code inside is executed.
Every time you call that generator function, you get a new generator object with
the initial state. The objects that are yielded inside the code don't show up yet.

The code inside the generator gets executed only when the generator object is
iterated over (or its .next() method is called). The objects that are yielded
are the results of calling the .next() method.


Maybe this will clarify it further.

def gen(n): .... while 1:
.... print 'before yield'
.... yield n
.... print 'after yield'
.... g = gen('hello')
g.next() before yield
'hello' g.next() after yield
before yield
'hello' g.next()

after yield
before yield
'hello'

When the next() method is called the generator runs until it reaches a
yield. At which point it's rests until the next() method is called again.

Although there are times when I wish it could run (as a thread) until it
reaches a yield and then continue after the next() method is called
until it reaches the next yield.

Cheers,
Ron



Mar 17 '06 #10
OK. I hope my understanding of the yield keyword and generators in a
general sense are now better understood. When a generator function is
assigned to an identifier, no code is executed and a generator is
immediately returned. When the next() method is called on the new
generator, code from top to bottom executes within the generator until
it reaches it's first yield. Many yields can appear within one
generator. When this is the case a next method call will execute code
from yield to yield. Code that appears in a loop after a yield keyword
is executed on the next() method call.

I hope I got it right. I love you guys for your patience and examples.
It is greatly appreciated and means very much to me! Thank you fellas!

Mar 18 '06 #11
vbgunz wrote:
OK. I hope my understanding of the yield keyword and generators in a
general sense are now better understood. When a generator function is
assigned to an identifier, no code is executed and a generator is
immediately returned. When the next() method is called on the new
generator, code from top to bottom executes within the generator until
it reaches it's first yield. Many yields can appear within one
generator. When this is the case a next method call will execute code
from yield to yield. Code that appears in a loop after a yield keyword
is executed on the next() method call.

I hope I got it right. I love you guys for your patience and examples.
It is greatly appreciated and means very much to me! Thank you fellas!


Yep, looks like you have it. ;-)

Only need to add what happens if a generator exits the end after the
yield(s).

If it were a function it would return a None object even if it didn't
have a return at the end. But a generator raises a StopIteration
Exception.
def gen(): .... yield 'hello'
.... print 'all done'
.... g = gen()
g.next() 'hello' g.next()

all done
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration

This is the signal to indicate iteration is finished. You don't see it
when you are using generators as iterators because it's usually caught
by the object or statement using the generator.

Cheers,
Ron

Mar 18 '06 #12

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

Similar topics

2
7012
by: Vilmar Brazão de Oliveira | last post by:
Hi all, What can be wrong in the database connection bellow to connect paradox? What is missing?? ----------------------------------------------------------------------------...
4
11346
by: TonyMontana | last post by:
Hello, I've the following problem. I've to read out the data of a custom application. I think (I'm not sure) this application is using a Paradox DB to store it's information. I don't know it...
0
6617
by: Steve Barker | last post by:
I am trying to access Paradox through a .NET Windows application I am writing. I have added a Data Connection to the Paradox table in the Server Explorer of Visual Studio .NET. I have used the...
4
4860
by: Eugen Walcher | last post by:
Hello all, I've tried posting this same question on other newsgroups with no luck. This group seems to have a lot more activity so I apologize if you have seen it before. I'm trying to...
0
1520
by: Naveen | last post by:
Hi, I have a Paradox 10 data entry application. I have developed some VB.Net forms to lookup data. I want to use these VB.Net search forms with Paradox forms. Some data must be exchanged between...
8
2197
by: Roger | last post by:
Has anyone done this yet? I know it is easily done in Paradox, but I would like to do it in VB.Net. Can anyone point me in the right direction? Thanks, Rog
0
1841
by: eselk | last post by:
I've got an old DOS Paradox database that I can't get rid of. We have way to many scripts and forms to convert everything at once. Instead we would like to be able to access the Paradox data from...
4
4530
by: eselk | last post by:
I've never really setup or used MS SQL Server (just a couple hours, one day, several months ago). I think MS SQL Server has the ability to use "linked tables", like MS Access does. Is this...
2
5286
by: John Navratil | last post by:
Anyone having any success with the paradox extension? 'pear install paradox' doesn't work. 'pecl install paradox' seem to do something, but none of the px_??? wrapper functions are found. I do...
0
7242
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
7138
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
7353
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,...
1
7075
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...
0
7508
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...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
446
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...

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.