The namespace for builtin functions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Blair Hall

    The namespace for builtin functions?

    Can anyone please tell me how to correctly use a built in function
    when there is a function of the same name in local scope?

    Here is an example. Suppose the following is in myApply.py:

    def apply(func,seq) :
    #
    # Code can default to
    # built-in definition in some cases:
    return __builtins__.ap ply(func,seq)

    #-------------------------------------
    if(__name__ == '__main__'):

    print "Three = ",apply(lam bda x,y: x+y, (1,2) )


    This seems to work, but if I import the definition of 'apply', like:
    [color=blue][color=green][color=darkred]
    >>> from myApply import apply
    >>> apply(lambda x,y: x+y, (1,2) )[/color][/color][/color]

    I get a crash:

    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    File "C:\proj_py\Lea rning\builtins\ myApply.py", line 5, in apply
    return __builtins__.ap ply(func,seq)
    AttributeError: 'dict' object has no attribute 'apply'

    I can't see what to use instead of '__builtins__' as the
    namespace for the built in functions.

  • Jay O'Connor

    #2
    Re: The namespace for builtin functions?

    Blair Hall wrote:
    [color=blue]
    > Can anyone please tell me how to correctly use a built in function
    > when there is a function of the same name in local scope?[/color]



    Save yourself a lot of trouble, just give it a different name.

    Comment

    • Peter Hansen

      #3
      Re: The namespace for builtin functions?

      Blair Hall wrote:[color=blue]
      >
      > Can anyone please tell me how to correctly use a built in function
      > when there is a function of the same name in local scope?
      >
      > Here is an example. Suppose the following is in myApply.py:
      >
      > def apply(func,seq) :
      > #
      > # Code can default to
      > # built-in definition in some cases:
      > return __builtins__.ap ply(func,seq)[/color]

      Try this instead:

      _builtin_apply = apply

      def apply(func, seq):
      return _builtin_apply( func, seq)

      But as Jay says, you're probably better off just not using
      the same name...

      -Peter

      Comment

      • Francis Avila

        #4
        Re: The namespace for builtin functions?

        Jay O'Connor wrote in message ...[color=blue]
        >Blair Hall wrote:
        >[color=green]
        >> Can anyone please tell me how to correctly use a built in function
        >> when there is a function of the same name in local scope?[/color]
        >
        >
        >
        >Save yourself a lot of trouble, just give it a different name.
        >[/color]

        This is the best advice.

        However...

        I don't understand why in __main__, the name __builtins__ refers to the
        module object __builtin__, but in any other namespace, __builtins__ is the
        __dict__ of __builtin__!

        E.g.:
        --- a.py ---
        a = lambda: __builtins__
        --- END ---
        [color=blue][color=green][color=darkred]
        >>> __builtins__[/color][/color][/color]
        <module '__builtin__' (built-in)>[color=blue][color=green][color=darkred]
        >>> id(__builtins__ )[/color][/color][/color]
        6577648[color=blue][color=green][color=darkred]
        >>> id(__builtins__ .__dict__)[/color][/color][/color]
        6584048[color=blue][color=green][color=darkred]
        >>> import a
        >>> id(a.a())[/color][/color][/color]
        6584048

        Why not 6577648?!
        --
        Francis Avila

        Comment

        • Fredrik Lundh

          #5
          Re: The namespace for builtin functions?

          Blair Hall wrote:
          [color=blue]
          > Can anyone please tell me how to correctly use a built in function
          > when there is a function of the same name in local scope?
          >
          > Here is an example. Suppose the following is in myApply.py:
          >
          > def apply(func,seq) :
          > #
          > # Code can default to
          > # built-in definition in some cases:
          > return __builtins__.ap ply(func,seq)[/color]

          the module is named __builtin__, and must be imported before
          it can be used.

          __builtins__ is a CPython implementation detail (it's used to cache
          a reference to the builtin modules, and are initialized on demand).

          for more info, see the "Overloadin g functions from the __builtin__
          module" here:



          </F>




          Comment

          • Bengt Richter

            #6
            Re: The namespace for builtin functions?

            On Sun, 30 Nov 2003 19:00:16 +0100, "Fredrik Lundh" <fredrik@python ware.com> wrote:
            [color=blue]
            >Blair Hall wrote:
            >[color=green]
            >> Can anyone please tell me how to correctly use a built in function
            >> when there is a function of the same name in local scope?
            >>
            >> Here is an example. Suppose the following is in myApply.py:
            >>
            >> def apply(func,seq) :
            >> #
            >> # Code can default to
            >> # built-in definition in some cases:
            >> return __builtins__.ap ply(func,seq)[/color]
            >
            >the module is named __builtin__, and must be imported before
            >it can be used.
            >
            >__builtins__ is a CPython implementation detail (it's used to cache
            >a reference to the builtin modules, and are initialized on demand).
            >
            >for more info, see the "Overloadin g functions from the __builtin__
            >module" here:
            >
            > http://effbot.org/zone/librarybook-builtin.htm[/color]
            Weird -- netscape 4.5 claims that document "contains no data"
            wget got it though. Maybe time to reboot windows ;-/[color=blue]
            >[/color]
            I think I agree with Francis. Why is __builtins__ set up
            in the interactive namespace differently from an imported
            module's namespace? To show what he was saying again, I made
            and empty module (nothing but an empty line in its source):
            [color=blue][color=green][color=darkred]
            >>> file('empty.py' ).read()[/color][/color][/color]
            '\n'[color=blue][color=green][color=darkred]
            >>> import empty
            >>> dir(empty)[/color][/color][/color]
            ['__builtins__', '__doc__', '__file__', '__name__']

            Ok, now in the interactive namespace:
            [color=blue][color=green][color=darkred]
            >>> __builtins__[/color][/color][/color]
            <module '__builtin__' (built-in)>

            And in the 'empty' module's namespace:
            [color=blue][color=green][color=darkred]
            >>> `empty.__builti ns__`[:60][/color][/color][/color]
            "{'help': Type help() for interactive help, or help(object) f"

            (I knew I would be getting the whole dict repr if I just typed empty.__builtin s__ ;-)
            [color=blue][color=green][color=darkred]
            >>> type(__builtins __)[/color][/color][/color]
            <type 'module'>[color=blue][color=green][color=darkred]
            >>> type(empty.__bu iltins__)[/color][/color][/color]
            <type 'dict'>[color=blue][color=green][color=darkred]
            >>> __builtins__.__ dict__ is empty.__builtin s__[/color][/color][/color]
            True[color=blue][color=green][color=darkred]
            >>>[/color][/color][/color]

            Seems inconsistent. Why not

            __builtins__ is empty.__builtin s__ => True

            and hence

            __builtins__.__ dict__ is empty.__builtin s__.__dict__ => True

            (or maybe both __builtins__ bindings could be to the dict, though that would
            bypass potential getattr magic that might be needed somewhere?)

            Regards,
            Bengt Richter

            Comment

            Working...