alternating the builtin functions

C

Carlo v. Dango

Hello there..

in the interactive shell in pythonWin I can type
True

but when I make this function

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


it fails with the error

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


What am I doing wrong here?
 
J

Jeff Epler

$ python<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:
http://python.org/doc/ref/exec.html#l2h-563

Jeff
 
J

Jp Calderone

Hello there..

in the interactive shell in pythonWin I can type

True

but when I make this function

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


it fails with the error

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


What am I doing wrong here?

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
 
C

Carlo v. Dango

$ python<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).

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 "orgIsInstance"
function pointer in the function... but it's a bit ugly IMHO


-carlo..
 
C

Carlo v. Dango

Trying to put something in __builtins__.

what do you mean?
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.

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
 
P

Peter Hansen

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

So just keep a reference to it.

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

_builtin_isinstance = isinstance

def carlo_isInstance(foo, bar):
# do some stuff that the original didn't do, then delegate
# to the original
return _builtin_isinstance(foo, bar)

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

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
 
C

Carlo v. Dango

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.

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__.isinstance() instead.. ;-)

-carlo
 
A

Alex Martelli

Carlo said:
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__.isinstance() instead.. ;-)

import __builtin__

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


Alex
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top