Reputation: 1071
I have a function that parses a file into a list. I'm trying to return that list so I can use it in other functions.
def splitNet():
network = []
for line in open("/home/tom/Dropbox/CN/Python/CW2/network.txt","r").readlines():
line = line.replace("\r\n", "")
line = string.split(line, ',')
line = map(int, line)
network.append(line)
return network
When I try to print the list outside of the function (for debugging) I get this error:
NameError: name 'network' is not defined
Is there something simple I am doing wrong or is there a better way to pass variables between functions without using globals?
Upvotes: 53
Views: 403824
Reputation: 729
L=[1, 2, 3]
def rl(l):
return l
[*ll] = rl(L) # ll is in a list
ll
# >>> [1, 2, 3]
*t, = rl(L) # ll is in a tuple
t
# >>> [1, 2, 3]
Upvotes: 1
Reputation: 1
If you want to return an item or list from a definition, you could define it before hand and use it as a variable during the initial writing of said definition. Unless it has to be defined within the definition. In this case you won't need to write in a return command at the end.
network = []
def splitNet(network):
for line in open("/home/tom/Dropbox/CN/Python/CW2/network.txt","r").readlines():
line = line.replace("\r\n", "")
line = string.split(line, ',')
line = map(int, line)
network.append(line)
print network # Will print the list you've appended. But it is now a usable object.
Upvotes: 0
Reputation: 11
You may declare the name of the variable assigned to the list as global
, like this:
def get_list():
global destination_list
destination_list = []
destination_list.extend(('1','2','3'))
return destination_list
get_list()
print(destination_list)
Upvotes: 1
Reputation: 132138
I assume you are not assigning the returned value to a variable in scope.
ie. you can't do
splitNet()
print network
instead you would
network = splitNet()
print network
or for that matter
my_returned_network_in_scope = splitNet()
print my_returned_network_in_scope
otherwise you could declare network outside of the splitNet function, and make it global, but that is not the recommended approach.
Upvotes: 18
Reputation: 288260
The names of variables in a function are not visible outside, so you need to call your function like this:
networks = splitNet()
print(networks)
A couple of other notes:
readlines
; the function itself is an iterator.with
statement.str.split
, which is more readable and easier to understand than string.split
.csv
module.In summary, this is how your code should look like:
import csv
def splitNet():
with open("/home/tom/Dropbox/CN/Python/CW2/network.txt") as nf:
for line in csv.reader(nf, delimiter=','):
yield map(int, line)
network = list(splitNet())
print (network)
Upvotes: 8
Reputation: 49597
Your function is returning a list so you have to assign it to a variable and than try to print it.
network = splitNet()
print network
For example
>>> def mylist():
... myl = []
... myl.append('1')
... return myl
...
>>> my_list = mylist()
>>> my_list
['1']
>>>
Upvotes: 5
Reputation: 47402
Have you actually called the function yet? This works fine (in the Python interpreter)
>>> def f():
... network = []
... network.append(1)
... network.append(2)
... network.append(3)
... return network
...
>>> network = f()
>>> print network
[1, 2, 3]
Upvotes: 2
Reputation: 13371
Variables cannot be accessed outside the scope of a function they were defined in.
Simply do this:
network = splitNet()
print network
Upvotes: 64