[Tutor] How to use a function

Peter Otten __peter__ at web.de
Mon Jan 19 16:08:20 CET 2015


jarod_v6 at libero.it wrote:

> Dear All,
> I would like to understand how work this funcion:
> 
> def parse_illumina_readset_file(illumina_readset_file):

[snip code by someone who has yet to learn how to use dicts;)]

>     return readsets
> 
> 
> parser_illumina_readset_file("~/Desktop/file.csv")


> I can't found the readsets list. Someone could please help me?
> thanks so much!

It looks like the function returns that list, but you discard it. Instead 
bind it to a name:

my_readset = parser_illumina_readset_file("~/Desktop/file.csv")
print(my_readset) # print the list or do anything you like with it.

Note: this is generally the right way to communicate with functions: have 
them return a value instead of modifying a global.

Wrong (I assume that this is what you expected):

a = []
def get_data():
   a.append(1) # placeholder for more complex operations
   a.append(2)

print(a)


Right (this is what you got):

def get_data():
    a = []
    a.append(1) # placeholder for more complex operations
    a.append(2)
    return a

a = get_data()
print(a)



More information about the Tutor mailing list