473,584 Members | 2,804 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 generatorFuncti on(sequence=['item1', 'item2', 'item3']):
for item in sequence:
yield item

yieldedValue = generatorFuncti on()

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

'''this is where things don't make any sense!'''
print '-' * 32
print generatorFuncti on() # <generator object at 0xb723022c>
print generatorFuncti on().next() # item1
print generatorFuncti on().next() # item1
print generatorFuncti on().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...
generatorFuncti on() 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 == generatorFuncti on() on a name basis?
I mean isn't the following technically the same?

generatorFuncti on()
yieldedValue = generatorFuncti on()

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 1734
Em Qui, 2006-03-16 Ã*s 16:17 -0800, vbgunz escreveu:
print generatorFuncti on() # <generator object at 0xb723022c>
print generatorFuncti on().next() # item1
print generatorFuncti on().next() # item1
print generatorFuncti on().next() # item1


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

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

yieldedValue = generatorFuncti on()
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.ne xt() # item1
print yieldedValue.ne xt() # item2
print yieldedValue.ne xt() # 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 generatorFuncti on() # <generator object at 0xb723022c> You're creating a new iterator here.
print generatorFuncti on().next() # item1 Another anonymous iterator gets created here. Instantly its next()
method is called, yielding first value.
print generatorFuncti on().next() # item1 And so on...
generatorFuncti on() 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 == generatorFuncti on() on a name
basis? I mean isn't the following technically the same?

generatorFuncti on()
yieldedValue = generatorFuncti on()


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.ne xt() 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 generatorFuncti on(sequence=['item1', 'item2', 'item3']):
for item in sequence:
yield item

yieldedValue = generatorFuncti on()

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

'''this is where things don't make any sense!'''
print '-' * 32
print generatorFuncti on() # <generator object at 0xb723022c>
print generatorFuncti on().next() # item1
print generatorFuncti on().next() # item1
print generatorFuncti on().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...
generatorFuncti on() 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 == generatorFuncti on() on a name basis?
I mean isn't the following technically the same?

generatorFuncti on()
yieldedValue = generatorFuncti on()

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 = generatorFuncti on()
b = generatorFuncti on()
print a.next()

item1

Now a and b are both generators for generatorFuncti on, but calling
a.next() again will return 'item2', while calling b.next() will return
'item1'. The value returned by generatorFuncti on 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 generatorFuncti on(sequence=['item1', 'item2', 'item3']):
for item in sequence:
yield item

yieldedValue = generatorFuncti on()

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

'''this is where things don't make any sense!'''
print '-' * 32
print generatorFuncti on() # <generator object at 0xb723022c>
print generatorFuncti on().next() # item1
print generatorFuncti on().next() # item1
print generatorFuncti on().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...
generatorFuncti on() 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 == generatorFuncti on() on a name basis?
I mean isn't the following technically the same?

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

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

Note the addresses are different.

Try this
b.next() 'item1' b.next() 'item2' generatorFuncti on().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 generatorFuncti on(sequence=['item1', 'item2', 'item3']):
for item in sequence:
yield item

yieldedValue = generatorFuncti on()

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

'''this is where things don't make any sense!'''
print '-' * 32
print generatorFuncti on() # <generator object at 0xb723022c>
print generatorFuncti on().next() # item1
print generatorFuncti on().next() # item1
print generatorFuncti on().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*********@gma il.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

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

Similar topics

2
7019
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?? ---------------------------------------------------------------------------- ------------------------- <% Response.Expires = -1 %> <% Dim ConParadox
4
11352
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 exactly cause I'm not familar with paradox at all, but I've found files like *.db, *.mb, *.px, *.xg*, *.yg* !!! I need the infomation from this files...
0
6629
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 "Microsoft OLD DB Provider for ODBC Drivers" option to make the connection. This works fine... even though the Paradox table is password protected. I...
4
4865
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 create a web service in c# using paradox data. The data is stored locally on my development machine with IIS also installed. Web services are working...
0
1522
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 the Paradox and VB.Net forms. I tried to use the .dll file created by Vb.Net project in the Paradox but couldn't create the instance of the...
8
2203
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
1845
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 MS Access, so that we can create a few select forms using Access. I've been trying for a while to get MS Access to work correctly with a linked...
4
4534
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 correct? What I want to do is have an MS SQL Server setup, which compatible applications can talk with, but I want the data to come from a Paradox...
2
5289
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 have /usr/share/pear in my include_path. Thanks, John Navratil
0
7829
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8330
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
7940
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
6587
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...
1
5705
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...
0
3824
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...
1
2336
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
1438
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1162
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.