Reputation: 91
I have this config file:
[test]
one: value1
two: value2
This function return items, values from section test of config file, but when I call the function only return first item (one, value1).
def getItemsAvailable(section):
for (item, value) in config.items(section):
return (item, value)
I call the getItemsAvailable() with this function:
def test():
item, value = getItemsAvailable('test')
print (item, value)
I guess that I should create a list on getItemsAvailable() function, and return the list for read the values on test() function, true?
Any suggestions?
Thanks!!
Upvotes: 4
Views: 114
Reputation: 29896
Use a generator function:
def getItemsAvailable(section):
for (item, value) in config.items(section):
yield (item, value)
And get the items like this:
def test():
for item, value in getItemsAvailable('test'):
print (item, value)
Upvotes: 1
Reputation: 17132
Use a list comprehension. Change
for (item, value) in config.items(section):
# the function returns at the end of the 1st iteration
# hence you get only 1 tuple.
# You may also consider using a generator & 'yield'ing the tuples
return (item, value)
to
return [(item, value) for item, value in config.items(section)]
And concerning your test()
function:
def test():
aList = getItemsAvailable('test')
print (aList)
Upvotes: 2