0

If I have a function defined in my code lets call foo(), and I use the following code:

from mod1 import *

With mod1 containing a function also with the same name of foo() and call the foo() function, will this override my original definition of the function when it evaluates it?

asked Dec 12, 2012 at 18:49
4
  • 1
    Is the function definition before or after the import? Commented Dec 12, 2012 at 18:51
  • 2
    (In general, this is why it's a good idea to avoid import *) Commented Dec 12, 2012 at 18:53
  • It's defined before the import, and yes this is just an exercise for one of my classes so I understand it is typically poor form Commented Dec 12, 2012 at 18:58
  • 1
    -1. Why can't you just test this yourself with some output, as a few of the answers did for you? Commented Dec 12, 2012 at 19:24

5 Answers 5

3

As far as I know it will.

you would need to either rename you foo() function you have built OR change your module input to read import mod1 and subsequently define any use of the foo() function from mod1 to mod1.foo()

jcjr
1,47425 silver badges40 bronze badges
answered Dec 12, 2012 at 18:55
Sign up to request clarification or add additional context in comments.

Comments

3

Depends on where the function is:

def foo():
 pass
from mod1 import *
foo() # here foo comes from mod1
# ------- 
# another interesting case
def foo():
 from mod1 import *
 foo() # this will also call foo from mod1
foo() # but this one is still the foo defined above. 
# ---------
# The opposite 
from mod1 import *
def foo():
 pass
foo() # here foo is the one defined above

Anyway, from module import * is considered a VERY bad and error-prone practice. It is a kind of using namespace std;-like thing in C++. Avoid it as much as possible.

answered Dec 12, 2012 at 18:55

Comments

1

a.py

def foo():
 print 'Hello A'

Test

>>> def foo():
... print 'Hello 1'
... 
>>> foo()
Hello 1
>>> from a import *
>>> foo()
Hello A
>>> def foo():
... print 'Hello 2'
... 
>>> foo()
Hello 2
answered Dec 12, 2012 at 18:57

Comments

1

I get the following:

file mod1.py contains

def foo():
 print "hello foo"

and then i start python interpreter and do the following:

>>> def foo():
... print "hello world"
... 
>>> from mod1 import *
>>> foo()
hello foo
>>>

So yes, it would override.

Unless you then do, a new

def foo():
 print "new foo"

In which case it would print "new foo"

answered Dec 12, 2012 at 18:55

Comments

0

It depends on the relative order of the function definition and the import statement. Whichever is executed second will override the first.

answered Dec 12, 2012 at 20:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.