alternating the builtin functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carlo v. Dango

    alternating the builtin functions

    Hello there..

    in the interactive shell in pythonWin I can type
    [color=blue][color=green][color=darkred]
    >>> i= 2
    >>> __builtins__.is instance(i, int)[/color][/color][/color]
    True

    but when I make this function

    def isinstance(obje ct, classtype):
    if __builtins__.is instance(object , classtype):
    return True
    else:
    if __builtins__.is instance(object , Wrapper): # if we are a
    wrapper look somewhere else
    return isinstance(obje ct.ref, classtype)
    return False


    it fails with the error

    File "foo.py", line xxx, in isinstance
    if __builtins__.is instance(object , classtype):
    AttributeError: 'dict' object has no attribute 'isinstance'


    What am I doing wrong here?

    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
  • Jeff Epler

    #2
    Re: alternating the builtin functions

    $ python[color=blue][color=green][color=darkred]
    >>> print __builtins__[/color][/color][/color]
    <module '__builtin__' (built-in)>

    $ python -c 'print __builtins__'

    $ echo 'print __builtins__ ' > m.py
    $ python -c 'import m'
    [contents of a dictionary]

    For some reason, __builtins__ has long been a different thing in
    __main__ than in any other module. To get consistent behavior, use
    'import __builtin__' (that's always a module).

    I think that the behavior/definition/value of __builtins__ is considered
    an implementation detail. The closest I found to an admission of this
    fact was here:
    The official home of the Python Programming Language


    Jeff

    Comment

    • Jp Calderone

      #3
      Re: alternating the builtin functions

      On Sun, Oct 19, 2003 at 02:44:50PM +0200, Carlo v. Dango wrote:[color=blue]
      > Hello there..
      >
      > in the interactive shell in pythonWin I can type
      > [color=green][color=darkred]
      > >>>i= 2
      > >>>__builtins__ .isinstance(i, int)[/color][/color]
      > True
      >
      > but when I make this function
      >
      > def isinstance(obje ct, classtype):
      > if __builtins__.is instance(object , classtype):
      > return True
      > else:
      > if __builtins__.is instance(object , Wrapper): # if we are a
      > wrapper look somewhere else
      > return isinstance(obje ct.ref, classtype)
      > return False
      >
      >
      > it fails with the error
      >
      > File "foo.py", line xxx, in isinstance
      > if __builtins__.is instance(object , classtype):
      > AttributeError: 'dict' object has no attribute 'isinstance'
      >
      >
      > What am I doing wrong here?
      > [/color]

      Trying to put something in __builtins__.

      Doing this just creates behavior which is surprising to people who read
      your program. Just define your version in one of your modules and leave it
      there. When you need it, import it.

      Jp


      Comment

      • Carlo v. Dango

        #4
        Re: alternating the builtin functions

        On Sun, 19 Oct 2003 08:56:15 -0500, Jeff Epler <jepler@unpytho nic.net>
        wrote:
        [color=blue]
        > $ python[color=green][color=darkred]
        >>>> print __builtins__[/color][/color]
        > <module '__builtin__' (built-in)>
        >
        > $ python -c 'print __builtins__'
        >
        > $ echo 'print __builtins__ ' > m.py
        > $ python -c 'import m'
        > [contents of a dictionary]
        >
        > For some reason, __builtins__ has long been a different thing in
        > __main__ than in any other module. To get consistent behavior, use
        > 'import __builtin__' (that's always a module).[/color]

        this yields no difference (python 2.3.2 windows) my solution right now is
        to write

        orgIsInstance = isinstance


        just before the function definition and then use the "orgIsInsta nce"
        function pointer in the function... but it's a bit ugly IMHO


        -carlo..


        --
        Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

        Comment

        • Carlo v. Dango

          #5
          Re: alternating the builtin functions

          On Sun, 19 Oct 2003 10:31:26 -0400, Jp Calderone <exarkun@intarw eb.us>
          wrote:[color=blue][color=green]
          >>
          >> File "foo.py", line xxx, in isinstance
          >> if __builtins__.is instance(object , classtype):
          >> AttributeError: 'dict' object has no attribute 'isinstance'
          >> What am I doing wrong here?
          >>[/color]
          > Trying to put something in __builtins__.[/color]

          what do you mean?
          [color=blue]
          >
          > Doing this just creates behavior which is surprising to people who read
          > your program. Just define your version in one of your modules and leave
          > it
          > there. When you need it, import it.[/color]

          sure, but my version relies on the original one, so I need a way to call
          the original isinstance() from within my isinstance()...


          -carlo -- olrac

          --
          Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

          Comment

          • Peter Hansen

            #6
            Re: alternating the builtin functions

            "Carlo v. Dango" wrote:[color=blue]
            >
            > sure, but my version relies on the original one, so I need a way to call
            > the original isinstance() from within my isinstance()...[/color]

            So just keep a reference to it.

            '''a module that has your isinstance in it:'''

            _builtin_isinst ance = isinstance

            def carlo_isInstanc e(foo, bar):
            # do some stuff that the original didn't do, then delegate
            # to the original
            return _builtin_isinst ance(foo, bar)

            # shadow the builtin name with your own, for the rest of this
            # module, if you like
            isinstance = carlo_isInstanc e

            Or whatever variation works for you...

            Actually, I think all Jp is really saying is don't use the name "isinstance "
            for your own... then none of this is an issue. When you want your
            version, you just call it. The original stays unperturbed.

            -Peter

            Comment

            • Carlo v. Dango

              #7
              Re: alternating the builtin functions

              > Actually, I think all Jp is really saying is don't use the name[color=blue]
              > "isinstance "
              > for your own... then none of this is an issue. When you want your
              > version, you just call it. The original stays unperturbed.[/color]

              for any normal application I would agree with you, but Im toying with the
              language on a lower level... thanks for your tip.. it works, although, I
              had prefered a solution such as being able to call
              __builtins__.is instance() instead.. ;-)

              -carlo



              --
              Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

              Comment

              • Alex Martelli

                #8
                Re: alternating the builtin functions

                Carlo v. Dango wrote:
                [color=blue][color=green]
                >> Actually, I think all Jp is really saying is don't use the name
                >> "isinstance "
                >> for your own... then none of this is an issue. When you want your
                >> version, you just call it. The original stays unperturbed.[/color]
                >
                > for any normal application I would agree with you, but Im toying with the
                > language on a lower level... thanks for your tip.. it works, although, I
                > had prefered a solution such as being able to call
                > __builtins__.is instance() instead.. ;-)[/color]

                import __builtin__

                (note, NO s in that identifier) and use __builtin__.isi nstance at will.
                (See p. 118 of "Python in a Nutshell" for a brief explanation).


                Alex

                Comment

                Working...