Pergunta

I had several problems knowing in which options I run a function, and I want to implement somehow that this functions prints its owns arguments. I found this question related, but I tried and didn't succeed. The code I tried:

def SharedData(list1, list2, list3, list4, list5, remove=False):
    """Do some magic"""
    sets=list1[:]
    #Magic happens
    print len(sets) / float(len(list1)) * 100, "% of genes start are equal between the predictors from", *list1

With this code I wanted to print the name of the first argument. So in the command line I know how I applied this function. I wanted to read at the terminal prompt something like that after running this function SharedData(glimmer, easygene, genemark, augustus, prodigal):

30,000000 % of genes start are equal between the predictors from glimmer

How can I achieve that?

Foi útil?

Solução

you could always do two things:

  • use only keyword args
  • decorate the function to print out the kwargs you passed in.

something like:

def printkwargs(func):
    @functools.wraps(func)
    def wrapper(**kwargs):
        ret = func(**kwargs)
        print ret, kwargs
    return wrapper

@printkwargs
def SharedData(...):...

i did not test this, but it should be ok

updated to include *args:

def printkwargs(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        ret = func(*args, **kwargs)
        print ret, args, kwargs
    return wrapper

by grabbing the locals dictionary:

from copy import copy

def f(a, b, c):
    l = copy(locals())
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top