3

So,I have a main.py and a file.py on file.py I have a function(ex:

def s_break(message):
     words = message.split(" ")

, and an array words. )

When I import the words array into main.py using: from "filename" import words I receive the array empty. Why?

Thank you!

2
  • 1
    You need to show more code I have no idea what are you doing and what could be the error Commented Feb 18, 2017 at 12:12
  • So, I have a function in file.py that splits a string in words, which are inserted in the array. Now, when I want to use that array in a different file, it comes empty. Commented Feb 18, 2017 at 12:55

1 Answer 1

4

You need to actually call the s_break function otherwise you'll just get the empty list/array.

test_file.py:

message = 'a sample string this represents'
list_of_words = []

def s_break(message):
    words = message.split(" ")
    for w in words:
        list_of_words.append(w)

s_break(message)    # call the function to populate the list

And then in main.py:

from test_file import list_of_words

print list_of_words

Output:

>>> ['a', 'sample', 'string', 'this', 'represents']
Sign up to request clarification or add additional context in comments.

1 Comment

thank you! It turned out I had to add at the parameter of s_break() the list_of_words array.

Your Answer

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.