473,698 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

generator object, next method

>>> gen = iterator()
gen.next <method-wrapper object at 0x009D1B70> gen.next <method-wrapper object at 0x009D1BB0> gen.next <method-wrapper object at 0x009D1B70> gen.next <method-wrapper object at 0x009D1BB0> gen.next is gen.next

False
What is behind this apparently strange behaviour? (The .next method
seems to alternately bind to two different objects)

Sw.

Sep 8 '05 #1
7 2494
si**********@gm ail.com wrote:
gen = iterator()
gen.next <method-wrapper object at 0x009D1B70>


Behind the scene, gen.next is bound to _, i. e. it cannot be
garbage-collected. Then...
gen.next <method-wrapper object at 0x009D1BB0>


a new method wrapper is created and assigned to _, and the previous method
wrapper is now garbage-collected. The memory location is therefore
available for reuse for...
gen.next <method-wrapper object at 0x009D1B70>


yet another method wrapper -- and so on.
gen.next <method-wrapper object at 0x009D1BB0> gen.next is gen.next False
What is behind this apparently strange behaviour? (The .next method
seems to alternately bind to two different objects)


But it isn't. What seems to be the same object are distinct objects at the
same memory location. See what happens if you inhibit garbage-collection by
keeping a reference of the method wrappers:
it = iter("")
[it.next for _ in range(5)]

[<method-wrapper object at 0x4029388c>, <method-wrapper object at
0x402938ec>, <method-wrapper object at 0x402938cc>, <method-wrapper object
at 0x4029390c>, <method-wrapper object at 0x4029392c>]

Peter

Sep 8 '05 #2
si**********@gm ail.com wrote:
gen = iterator()
gen.next<method-wrapper object at 0x009D1B70> gen.next<method-wrapper object at 0x009D1BB0> gen.next<method-wrapper object at 0x009D1B70> gen.next<method-wrapper object at 0x009D1BB0> gen.next is gen.next

False
What is behind this apparently strange behaviour? (The .next method
seems to alternately bind to two different objects)


It is a combination of factors.

1) Every time you access gen.next you create a new method-wrapper object.
2) Typing an expression at the interactive prompt implicitly assigns the
result of the expression to the variable '_'
3) When the method-wrapper is destroyed the memory becomes available to be
reused the next time a method-wrapper (or other object of similar size) is
created.

So in fact you have 6 different objects produced by your 6 accesses to
gen.next although (since you never have more than 3 of them in existence at
a time) there are probably only 3 different memory locations involved.
Sep 8 '05 #3
Duncan Booth <du**********@i nvalid.invalid> writes:
1) Every time you access gen.next you create a new method-wrapper object.


Why is that? I thought gen.next is a callable and gen.next() actually
advances the iterator. Why shouldn't gen.next always be the same object?
Sep 8 '05 #4
Why is that? I thought gen.next is a callable and gen.next() actually
advances the iterator. Why shouldn't gen.next always be the same object?


That is, in essence, my question.

Executing the below script, rather than typing at a console, probably
clarifies things a little.

Sw.

#-------------------
def iterator():
yield None

gen = iterator()

#gen.next is bound to x, and therefore, gen.next should not be GC?
x = gen.next
y = gen.next
print x
print y
print gen.next
#-------------------

Sep 8 '05 #5
si**********@gm ail.com wrote:
Why is that? I thought gen.next is a callable and gen.next() actually
advances the iterator. Why shouldn't gen.next always be the same object?

That is, in essence, my question.


Because bound methods are generated on the fly - google this group,
there have been plenty of discussions about that.

Diez
Sep 8 '05 #6
Paul Rubin wrote:
Duncan Booth <du**********@i nvalid.invalid> writes:
1) Every time you access gen.next you create a new method-wrapper
object.


Why is that? I thought gen.next is a callable and gen.next() actually
advances the iterator. Why shouldn't gen.next always be the same
object?


It is a consequence of allowing methods to be first class objects, so
instead of just calling them you can also save the bound method in a
variable and call it with the 'self' context remembered in the method.

It is easier to see what is happening if you look first at ordinary Python
classes and instances:
class C: def next(self): pass

c = C()
c.next <bound method C.next of <__main__.C instance at 0x00B4D6E8>> C.next <unbound method C.next>

Here, 'next' is a method of the class C. You can call the unbound method,
but then you have to explicitly pass the 'self' argument.

Whenever you access the method through an instance it creates a new 'bound
method' object which stores references to both the original function, and
the value to be passed in as the first parameter. Usually this object is
simply called and discarded, but you can also save it for later use.

Python could perhaps bypass the creation of bound method objects when
calling a function directly, but it would still need them for cases where
the method isn't called immediately (and it isn't obvious it would be an
improvement if it tried to optimise this case).

It would be possible for a language such as Python to try to either
generate these bound method objects in advance (which would be horribly
inefficient if you created lots of objects each of which had hundreds of
methods which were never called), or to cache bound method objects so as to
reuse them (which would be inefficient if you have lots of methods called
only once on each object). Python chooses to accept the hit of creating
lots of small objects, but tries to make the overhead of this as low as
possible (which is one reason the memory gets reused immediately).

The next method in a generator works in the same way as bound methods,
although the actual types involved are C coded. You can still access both
the bound and unbound forms of the next method. The bound form carries the
information about the first parameter, and the unbound form has to be given
that information:
gen = iterator()
gen.next <method-wrapper object at 0x00B43E30> type(gen).next <slot wrapper 'next' of 'generator' objects> gen.next() 'hi' type(gen).next( gen)

'hi'
Sep 8 '05 #7

"Paul Rubin" <"http://phr.cx"@NOSPAM. invalid> wrote in message
news:7x******** ****@ruckus.bro uhaha.com...
Duncan Booth <du**********@i nvalid.invalid> writes:
1) Every time you access gen.next you create a new method-wrapper
object.


Why is that? I thought gen.next is a callable and gen.next() actually
advances the iterator. Why shouldn't gen.next always be the same object?


If you explicitly or implicitly (via for loop) calculate gen.next exact
once (as I presume for loops do, and as I would for explicit while loop),
then it is. When you keep a reference to the wrapper, and call it
repeatedly via that wrapper, then all is as you expect.

next_x = genfunc(*args). next
while True:
x = next_x() # same next_x each time
<do something with x>

Terry J. Reedy

Sep 8 '05 #8

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

Similar topics

9
2686
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as open, even though it's at least a year old and Guido doesn't seem very hot on the idea). I'm not too sure of its ideas on raising exceptions in generators from outside (although it looks like it might be convenient in some cases), but being able...
17
2431
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next = gen.next() finally:
8
2305
by: Paul Chiusano | last post by:
I've been playing around with generators and have run into a difficulty. Suppose I've defined a Node class like so: class Node: def __init__(self, data=None, left=None, right=None): self.children = self.children.append(left) self.children.append(right) self.data = data
45
3036
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
12
1948
by: Thomas Lotze | last post by:
Hi, I'm trying to figure out what is the most pythonic way to interact with a generator. The task I'm trying to accomplish is writing a PDF tokenizer, and I want to implement it as a Python generator. Suppose all the ugly details of toknizing PDF can be handled (such as embedded streams of arbitrary binary content). There remains one problem, though: In order to get random file access, the tokenizer should not simply spit out a series...
5
2252
by: Jerzy Karczmarczuk | last post by:
I thought that the following sequence gl=0 def gen(x): global gl gl=x yield x s=gen(1)
41
2518
by: Petr Jakes | last post by:
Hello, I am trying to study/understand OOP principles using Python. I have found following code http://tinyurl.com/a4zkn about FSM (finite state machine) on this list, which looks quite useful for my purposes. As this code was posted long time ago (November 1998) I would like to ask if the principles used in this code are still valid in the "modern" Python and if/how it can be improved (revrited) using futures of current version of...
11
1745
by: vbgunz | last post by:
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=): for item in sequence: yield item
3
1148
by: Alan Isaac | last post by:
Essentially I want a generator that I can query about its characteristics. (E.g., a random number generator that I want to be able to ask about is distributional parameters.) I am thinking of a class that wraps a generator. An object of this class will have a ``next`` method that simply returns the value the object get by calling the wrapped generator.
0
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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
7739
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
6528
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
5862
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
3
2007
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.