Adding objects to __builtins__

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matteo Merli

    Adding objects to __builtins__


    Hi,
    is there a way to save objects in the __builtins__ namespace?
    the goal is to make them available in all the modules of my app, without
    reimporting all the things and keep references...

    The curious thing is that when i try from the interactive interpreter the
    thing just works:
    [color=blue][color=green][color=darkred]
    >>> def a():[/color][/color][/color]
    .... print "ciao"
    ....[color=blue][color=green][color=darkred]
    >>> a()[/color][/color][/color]
    ciao[color=blue][color=green][color=darkred]
    >>> __builtins__.b = a
    >>> b()[/color][/color][/color]
    ciao[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    But i've tried inside a script, recalling objects from other imported
    modules, they cannot see them.
    Does exist a way to do this, or is just a stupid thing?

    Thank you
    matteo merli

  • Peter Hansen

    #2
    Re: Adding objects to __builtins__

    Matteo Merli wrote:
    [color=blue]
    > is there a way to save objects in the __builtins__ namespace?
    > the goal is to make them available in all the modules of my app, without
    > reimporting all the things and keep references...
    >
    > The curious thing is that when i try from the interactive interpreter the
    > thing just works:[/color]

    Does this help?
    [color=blue][color=green][color=darkred]
    >>> import __builtins__[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ImportError: No module named __builtins__[color=blue][color=green][color=darkred]
    >>> import __builtin__[/color][/color][/color]

    By the way, you *really* shouldn't use this as a "convenient "
    way of getting global variables or other things around to your
    various modules. That would be almost the worst thing for
    code maintainability that you could do. You'll come to hate
    yourself for doing it, down the road.

    There are some valid use cases for doing such a thing, but
    sticking just any old function in there is not it.

    -Peter

    Comment

    Working...