473,398 Members | 2,393 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,398 software developers and data experts.

Question on importing and function defs

TC
I have a problem. Here's a simplified version of what I'm doing:

I have functions a() and b() in a module called 'mod'. b() calls a().

So now, I have this program:

from mod import *

def a():
blahblah

b()
The problem being, b() is calling the a() that's in mod, not the new
a() that I want to replace it. (Both a()'s have identical function
headers, in case that matters.) How can I fix this?

Thanks for any help.
Mar 2 '08 #1
2 1066
TC wrote:
I have a problem. Here's a simplified version of what I'm doing:

I have functions a() and b() in a module called 'mod'. b() calls a().

So now, I have this program:

from mod import *

def a():
blahblah

b()
The problem being, b() is calling the a() that's in mod, not the new
a() that I want to replace it. (Both a()'s have identical function
headers, in case that matters.) How can I fix this?

Thanks for any help.
The only problem I could see with this is a local function issue.
Meaning that even when you write the new a() function in the main source
file, b() doesn't know it exists because it's relying on it's own
"local" a() in the module. I'm also new to using Python so I that's all
I can think would be the problem.

Nick

Mar 2 '08 #2
TC wrote:
I have a problem. Here's a simplified version of what I'm doing:

I have functions a() and b() in a module called 'mod'. b() calls a().

So now, I have this program:

from mod import *

def a():
blahblah

b()
The problem being, b() is calling the a() that's in mod, not the new
a() that I want to replace it. (Both a()'s have identical function
headers, in case that matters.) How can I fix this?

Thanks for any help.
Since b calls mod.a, you could replace mod.a with your new a. Like
this: (Warning, this could be considered bad style because it will
confuse anyone who examines the mod module in an attempt to understand
you code.)
import mod

def replacement_a():
...

mod.a = replacement_a

...
Or another option. Define b to take, as a parameter, the "a" function
to call.

In mod:

def a():
...

def b(fn=a): # to set the default a to call
...

And you main program:

from mod import *

def my_a():
...

b(my_a)
Hope that helps

Gary Herron

Mar 2 '08 #3

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

Similar topics

11
by: Jeff Wagner | last post by:
I am importing a file which contains a persons name (firstName, middleName, etc). If I define a function to do this, how can I use the variables outside of that function? Here is the code: ...
1
by: Anthony Liu | last post by:
Hi, I think I got confused by the python import facility. Say, in code1.py I have func1, func2, func3 and main. In code2.py, I *only* want to use func2 from code1.py. So, I did
0
by: Putz Ronald | last post by:
Hy! I am trying to convert a win Oracle application to solaris! We have use external c functions which I have ibncluded in a shared object. See the makefile below: OBJECTS = definitions.o...
1
by: torjon | last post by:
I have a text file with 180 headers. Is it possible to import this file into mysql where mysql will automatically create the table with column headings from the headers (as sql server and access...
1
by: Dan | last post by:
Could someone please help me with auto importing a series of data files into an Access table. I tried to follow code given below in a previous messagebut i'm getting error messages. Here's my...
2
by: G Patel | last post by:
aan anyone tell me what a .defs and .s file extensions are in a C program (folder)? thanks a lot
6
by: val bykoski | last post by:
Hi The List: I have a modeling app where i'm detecting events (in temporal dynamics) applying a set of (boolean) functions - kind of: event_list = "f1 f2 etc".split() # each fi detects a...
7
by: biner.sebastien | last post by:
I have a problem understanding the scope of variable in nested function. I think I got it nailed to the following example copied from Learning Python 2nd edition page 205. Here is the code. def...
3
by: int32bit | last post by:
I can't figure out why this doesn't work. Any ideas appreciated. conn = MySQLdb.connect (db = "vocab") cursor = conn.cursor () cursor.execute ("SELECT VERSION()") row = cursor.fetchone ()...
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.