Overriding a function...

P

programmer.py

Suppose I have this module `mymodule.py' -

# mymodule.py - begin
def test():
print "original"
# mymodule.py - end

Now assume that I do this in some arbitrary module ->

def override():
print "test is overridden"

import mymodule
mymodule.test = override

Two questions -

1) If mymodule is subsequently imported, will my `override' function be
used, or will python magically `fix' (or break depending on your
perspective) mymodule and use the original mymodule.test function?

2) Once I assign mymodule.test with override, will modules that
imported my module *PRIOR* to the assignment get override, or will they
keep the original function test()?

I hope that makes sense.

Thanks!
jw
 
M

Marc 'BlackJack' Rintsch

In <[email protected]>,
1) If mymodule is subsequently imported, will my `override' function be
used, or will python magically `fix' (or break depending on your
perspective) mymodule and use the original mymodule.test function?

It will not `fix` it. Importing an already imported module doesn't reload
the module.
2) Once I assign mymodule.test with override, will modules that
imported my module *PRIOR* to the assignment get override, or will they
keep the original function test()?

They see the `override()` function.

Ciao,
Marc 'BlackJack' Rintsch
 
B

benboals

Suppose I have this module `mymodule.py' -

# mymodule.py - begin
def test():
print "original"
# mymodule.py - end

Now assume that I do this in some arbitrary module ->

def override():
print "test is overridden"

import mymodule
mymodule.test = override

Two questions -

1) If mymodule is subsequently imported, will my `override' function be
used, or will python magically `fix' (or break depending on your
perspective) mymodule and use the original mymodule.test function?

2) Once I assign mymodule.test with override, will modules that
imported my module *PRIOR* to the assignment get override, or will they
keep the original function test()?

I hope that makes sense.

Thanks!
jw

iirc, python only imports modules once, so for future imports should be
ok with the override. There is a reload() function that wipes and
reloads a module(though not, I believe, recursively, that is, not
modules that it imported) I've done someting similar to your
overwriting in the past, anyway, and it worked.

However, you should find this easy to test yourself, in case the
revision makes a difference.
How i'd test it:

file: original
def afunction(): print 'Hi'

file: changer
import original
def otherFunc(): print "Bye"
original.afunction()=otherFunc

file: final1
import original
original.afunction() ##should say hi
import changer
original.afunction() ##should say bye
changer.original.afunction() ##should still say bye.


file: final2
import changer
changer.original.afunction() ##should say bye
import original
changer.original.afunction() ##should say bye
original.afunction() ##should say bye

cmd prompt:
python final1.py
Expect:
Hi
Bye
Bye

python final2.py
Expect:
Bye
Bye
Bye
 
K

K.S.Sreeram

Marc said:
They see the `override()` function.

That depends on how the import was done.

If you do 'import mymodule' and then use 'mymodule.test' to access the
function, then they'll see the new 'override' function.

Alternatively, If you do 'from mymodule import test', and use 'test',
then they'll see the original 'test' function.

Regards
Sreeram


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFElvvDrgn0plK5qqURAlquAJ4qY0t28YykJv7cIruAjXG5LeXV+QCaAm+B
hMfk50HK8XmjdV/JB8oyswM=
=m2RR
-----END PGP SIGNATURE-----
 

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,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top