inconsistent value from __builtins__

Discussion in 'Python' started by Michael Hohn, Oct 1, 2004.

  1. Michael Hohn

    Michael Hohn Guest

    Hi,
    using the file builtin_check.py with content

    # Module builtin_check
    # Inconstency in the binding of __builtins__

    def get_binding(name):
    return locals()[name]

    def get_global_binding(name):
    return globals()[name]

    and running

    import builtin_check
    print type(builtin_check.get_global_binding('__builtins__'))
    print type(__builtins__)

    in the toplevel, I get the following results:

    Python 2.2.3 (#2, Jun 16 2004, 21:14:24)
    [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import builtin_check
    >>> print type(builtin_check.get_global_binding('__builtins__'))

    <type 'dict'>
    >>> print type(__builtins__)

    <type 'module'>


    Python 2.3.3 (#1, May 10 2004, 11:29:59)
    [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import builtin_check
    >>> print type(builtin_check.get_global_binding('__builtins__'))

    <type 'dict'>
    >>> print type(__builtins__)

    <type 'module'>

    Is this difference in return value intentional?


    Thanks,
    Michael
     
    Michael Hohn, Oct 1, 2004
    #1
    1. Advertisements

  2. Michael Hohn <> wrote:
    ...
    > >>> print type(builtin_check.get_global_binding('__builtins__'))

    > <type 'dict'>
    > >>> print type(__builtins__)

    > <type 'module'>

    ...
    > Is this difference in return value intentional?


    Well, it's _documented_ that '__builtins__' can be either a dictionary
    or a module, and it's been that way for a long time. Whether it's
    intentional (or sensible), I don't know. In any case, the idea is that
    if you need to access the built-in namespace directly, you should start
    with "import __builtin__" (note, no 's') which will definitely give you
    a module. Yeah, it _is_ somewhat confusing:-(.


    Alex
     
    Alex Martelli, Oct 1, 2004
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Opinder
    Replies:
    1
    Views:
    554
    Scott David Daniels
    Mar 2, 2004
  2. Remy Blank
    Replies:
    3
    Views:
    1,170
    Lothar Scholz
    Mar 6, 2004
  3. Jacek Generowicz
    Replies:
    1
    Views:
    479
    Daniel Dittmar
    Apr 8, 2004
  4. Matteo Merli

    Adding objects to __builtins__

    Matteo Merli, Jun 2, 2004, in forum: Python
    Replies:
    1
    Views:
    397
    Peter Hansen
    Jun 2, 2004
  5. Laszlo Zsolt Nagy

    __builtins__ wreidness

    Laszlo Zsolt Nagy, Apr 8, 2005, in forum: Python
    Replies:
    0
    Views:
    339
    Laszlo Zsolt Nagy
    Apr 8, 2005
  6. Michael Hoffman

    Proposal: add sys to __builtins__

    Michael Hoffman, Sep 1, 2005, in forum: Python
    Replies:
    21
    Views:
    722
    Tom Anderson
    Sep 26, 2005
  7. Collin Winter

    Single type for __builtins__ in Py3.0

    Collin Winter, Sep 23, 2005, in forum: Python
    Replies:
    4
    Views:
    477
    Collin Winter
    Sep 23, 2005
  8. Collin Winter

    Re: Single type for __builtins__ in Py3.0

    Collin Winter, Sep 23, 2005, in forum: Python
    Replies:
    4
    Views:
    440
    Collin Winter
    Sep 24, 2005
Loading...