python - Function that replaces itself with builtin after first execution -


is possible write function replaces after first execution?

first.py:

from second import max x = max(1,2,3) y = max(4,5,6) 

second.py:

def max(*args):     ... #rewire references     return min(args) 

can make second call max calls builtin, while first 1 calls 1 second.py? tried modifying globals() dict, had no effect, of course, neither did declaring global max in custom max function, renamed (and later named max again).

is there way possible? can access importing module in way?

(before comes inevitable comment: know bad idea, don't plan use in production, if it's possible. i'm interested in knowing whether doable.)

edit: clarify, afterwards should how looks in repl:

>>> second import * >>> max <function max @ 0x10e501400> >>> x = max(1,2,3) >>> max <built-in function max> 

in file maxtest.py:

import __builtin__  _max = __builtin__.max def max(*args):     print "inside non standard max function"     __builtin__.max = _max     return min(args)  __builtin__.max = max     

in file maxtest2.py:

import maxtest  x = max(1, 2, 3) y = max(4, 5, 6) 

running python maxtest2.py produces:

inside non standard max function 

note output happens once, indicated max function has been changed second call.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -