PDA

View Full Version : Help with importing functions from another file in Python



Tradewind
November 8th, 2009, 04:59 AM
Hello,
I need help on importing functions so they can be used in a different file. For example, if I have a file A, that looks something like this:


def return_something(text, list_of_text):

if text in list_of_text:
if text = 'Yes':
print 'True'
else:
print 'False'
else:
print 'None'

And in another file B, I'll request user input through raw_input, and use the function "return_something" from A to process the text in B, so either "True" or "False" is printed in file B, how do I do that? "return_something" needs two parameters, but in file B all I want the user to do is input one string, and the program analyzes that string with the list_of_text in file A, processes it with the if/print statements, then display the result back in file B.

Thanks!

themusicwave
November 8th, 2009, 04:36 PM
If you want to use things from another file you need to use import. Check out this link for details:

http://effbot.org/zone/import-confusion.htm

Tradewind
November 8th, 2009, 04:48 PM
Hello I realize I have to use import, but I was confused about the two parameters. In the second file all the user does is input one line of text, but the function requires two parameters. How do I call the function from file A in file B after importing it? Is it return_something('user text here', list_of_text)? But would file B recognize what "list_of_text" is, if it's defined in file A?

themusicwave
November 8th, 2009, 04:54 PM
#File A.py
def foo(arg):
print arg

#File B.py
import A

A.foo("Test")


That is how you call a function in A from B with an argument. When the foo function gets called, whatever value you give it gets passed in as arg.

Tradewind
November 9th, 2009, 11:51 PM
I need to use the same variable as the one found in file A. How do I write that if I want to call that function in file B?
E.g., in file A:

#a is some string, b is a list of strings
def func(a, b):
if a in b:
print 'True'
else:
print 'False'File B:

import A

answer = raw_input('please enter string for variable a: ')

#then call the function 'func' in file A to analyze the string 'answer', then print either True or False

themusicwave
November 10th, 2009, 01:54 AM
I think you want something like this:



import A

answer = raw_input('please enter string for variable a: ')
words = ['word1','word2','word3']
value = A.func(words, answer)
print value


You actually don't need value I just did that for readability. you could do this instead:



import A

answer = raw_input('please enter string for variable a: ')
words = ['word1','word2','word3']
print A.func(words, answer)


Also if this is python 3.0 make sure you use print as a function instead like print( A.func(answer, words) )

Hope that helps.

Tradewind
November 10th, 2009, 03:40 AM
The problem is the list is not fixed, it's a variable. I cannot set what the list is, because the user enters it, so I need to treat it like a variable. I need a way to import that variable into the second file for use.

delfick
November 10th, 2009, 12:21 PM
like this ??

FILE A.py----------


from B import func

numItems = raw_input("Enter the number of items in your list : ")
try:
numItems = int(numItems)
except ValueError:
numItems = None
print "You didn't enter an integer, exiting now"

if numItems:
items = []
for item in range(numItems):
next = raw_input("Enter the next item : ")
items.append(next)

print "You've entered %s" % items

varA = raw_input("Please enter a value for variable a : ")

func(varA, items)


FILE B.py---------


def func(a, b):
print a in b

Tradewind
November 10th, 2009, 05:13 PM
Hello,
That won't work as I already have what the list is in the first file, it's actually one of the return values of a really complex function. I need to directly import that variable into the second file, I cannot redo the entire function in first file to define what that variable is again in second file.
E.g.:
First file:

def complex_function(parameters):

#lots of code

a_tuple = (d1, d2, a)
return a_tuple


Second file:

#Now I need to import "d1" and "d2" (since they're dictionaries) into another file. So I can write stuff like:


x = d1[0] #first term in dictionary from first file

delfick
November 11th, 2009, 12:29 AM
I get this feeling you don't fully understand what the computer is actually doing here.

I can't really give a decent explanation (hopefully someone will expand) but...
(edit: infact, reading it, I can see how that might not make much sense, sorry :p)

Having code in different files is only a logical separation of code, as in it's only for the sake of humans. (Well, there are scope rules : http://en.wikipedia.org/wiki/Scope_(programming) ) but at the end of the day. at the lowest level, it isn't running from the file itself.

When you pass a function some arguments then it doesn't matter which file you've created those variables in, it all ends up on the stack (http://en.wikipedia.org/wiki/Call_stack). Well, references to them anyways.

so you can do something like this

FILE A.py----------


from B import func

def funcThatCreatesList():
return ['yes', 'no', 'maybe']


items = funcThatCreatesList()
varA = raw_input("Please enter a value for variable a : ")
func(varA, items)


FILE B.py---------


def func(a, b):
print a in b


...

:)

Tradewind
November 11th, 2009, 05:27 AM
Hello,
Thanks I figured out the problem I had. I just needed to re-define each parameter of the function of file A in file B, then I can successfully call that function, and even variables inside that function.