473,385 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Overriding a function...

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

Jun 19 '06 #1
3 7634
In <11**********************@p79g2000cwp.googlegroups .com>,
pr***********@gmail.com wrote:
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
Jun 19 '06 #2

pr***********@gmail.com wrote:
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

Jun 19 '06 #3
Marc 'BlackJack' Rintsch wrote:
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.


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

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

Jun 19 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
9
by: James Marshall | last post by:
I'm writing a library where I want to override document.write(), but for all document objects; thus, I want to put it in the prototype. I tried Document.prototype.write= my_doc_write ; but it...
8
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class)...
1
by: Xiangliang Meng | last post by:
Hi, all. When reading C++ books, I'm alway confused by those terms "redefining functions", "overloading functions" and "overriding functions". Please give me some comments on those terms....
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
11
by: iceColdFire | last post by:
Hi, What is the Diff btwn Function overloading and overriding thanks, a.a.cpp
15
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
1
by: Raghu | last post by:
Hallo Wo kann ich online material fur Function overriding finden? Mit freundlichen Gruessen Raghu.
12
by: danil52 | last post by:
Hello there, I have the following code: class Base { public: virtual void f() {cout << "Base::f()" << endl;} virtual void f(int) {cout << "Base::f(int)" << endl;} };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.