Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9283021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:31:26+00:00 2026-06-18T18:31:26+00:00

Many iterator functions in the __builtin__ module are actually implemented as types, even although

  • 0

Many iterator “functions” in the __builtin__ module are actually implemented as types, even although the documentation talks about them as being “functions”. Take for instance enumerate. The documentation says that it is equivalent to:

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1

Which is exactly as I would have implemented it, of course. However, I ran the following test with the previous definition, and got this:

>>> x = enumerate(range(10))
>>> x
<generator object enumerate at 0x01ED9F08>

Which is what I expect. However, when using the __builtin__ version, I get this:

>>> x = enumerate(range(10))
>>> x
<enumerate object at 0x01EE9EE0>

From this I infer that it is defined as

class enumerate:
    def __init__(self, sequence, start=0):
        # ....

    def __iter__(self):
        # ...

Rather than in the standard form the documentation shows. Now I can understand how this works, and how it is equivalent to the standard form, what I want to know is what is the reason to do it this way. Is it more efficient this way? Does it has something to do with these functions being implemented in C (I don’t know if they are, but I suspect so)?

I’m using Python 2.7.2, just in case the difference is important.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T18:31:28+00:00Added an answer on June 18, 2026 at 6:31 pm

    Yes, it has to do with the fact that built-ins are generally implemented in C. Really often C code will introduce new types instead of plain functions, as in the case of enumerate.
    Writing them in C provide finer control over them and often some performance improvements,
    and since there is no real downside it’s a natural choice.

    Take into account that to write the equivalent of:

    def enumerate(sequence, start=0):
        n = start
        for elem in sequence:
            yield n, elem
            n += 1
    

    in C, i.e. a new instance of a generator, you should create a code object that contains the actual bytecode. This is not impossible, but is not so easier than writing a new type which simply implements __iter__ and __next__ calling the Python C-API, plus the other advantages of having a different type.

    So, in the case of enumerate and reversed it’s simply because it provides better performance, and it’s more maintainable.

    Other advantages include:

    • You can add methods to the type(e.g. chain.from_iterable). This could be done even with functions, but you’d have to first define them and then manually set the attributes, which doesn’t look so clean.
    • You can us isinstance on the iterables. This could allow some optimizations(e.g if you know that isinstance(iterable, itertools.repeat), then you may be able to optimize the code since you know which values will be yielded.

    Edit: Just to clarify what I mean by:

    in C, i.e. a new instance of a generator, you should create a code
    object that contains the actual bytecode.

    Looking at Objects/genobject.c the only function to create a PyGen_Type instance is PyGen_New whose signature is:

    PyObject *
    PyGen_New(PyFrameObject *f)
    

    Now, looking at Objects/frameobject.c we can see that to create a PyFrameObject you must call PyFrame_New, which has this signature:

    PyFrameObject *
    PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
                PyObject *locals)
    

    As you can see it requires a PyCodeObject instance. PyCodeObjects are how the python interpreter represents bytecode internally(e.g. a PyCodeObject can represent the bytecode of a function), so: yes, to create a PyGen_Type instance from C you must manually create the bytecode, and it’s not so easy to create PyCodeObjects since PyCode_New has this signature:

    PyCodeObject *
    PyCode_New(int argcount, int kwonlyargcount,
               int nlocals, int stacksize, int flags,
               PyObject *code, PyObject *consts, PyObject *names,
               PyObject *varnames, PyObject *freevars, PyObject *cellvars,
               PyObject *filename, PyObject *name, int firstlineno,
               PyObject *lnotab)
    

    Note how it contains arguments such as firstlineno, filename which are obviously meant to be obtained by python source and not from other C code. Obviously you can create it in C, but I’m not at all sure that it would require less characters than writing a simple new type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to get the position of an iterator (how many times
Boost's any_range documentation says the following: Despite the underlying any_iterator being the fastest available
After writing so many for loop, I could not find to name iterator. I
I am writing an iterator for a container which is being used in place
In Python3 many methods are returning iterator or generator objects (instead of lists or
In the Open Babel library, there are defined many iterator objects for OBMol class,
many Places in the sample code i have seen 2 different way of @synthesize
Many threads have access to summary . Each thread will have an unique key
Many people use this method to add animation on switching views. When I try
Many search engines are not using meta keywords and Google through many recent updates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.