0

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?

Community
  • 1
  • 1
llrs
  • 3,308
  • 35
  • 68
  • I do not think that is possible. When the function is called "glimmer" is replaced with its value. The interpreter doesn't care what it is called, and never tells the function. The function only gets the value. – rspencer Mar 12 '14 at 13:14
  • Let me check if understand you correct, you seem to want to know the name of the variable which was used as an argument? If so, that's not possible in any simple way. Besides what if the function is called as `SharedData(f(1) + g('x'), ...)`? – bereal Mar 12 '14 at 13:19
  • Right. It should not be called like that but then I want to be printed "... from f(1) + g('x')" – llrs Mar 12 '14 at 13:24

1 Answers1

1

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())
acushner
  • 9,595
  • 1
  • 34
  • 34
  • Mmm, even after reading the [documentation](http://docs.python.org/2/library/functools.html) I have problems. Could you please explain it a little? And I have some problems: `TypeError: wrapper() takes exactly 0 arguments (5 given)` when applying this code – llrs Mar 12 '14 at 13:32
  • 1
    *use only keyword args* i.e. SharedData(a=5, b=6), not SharedData(5, 6) – acushner Mar 12 '14 at 13:57
  • i only did it this way because you want the names of the parameters, if you don't care about parameter names, you could include *args. also check this out. http://stackoverflow.com/questions/218616/getting-method-parameter-names-in-python – acushner Mar 12 '14 at 13:58
  • With the updated version I get the object itself, the list with the values inside. With the question you pointed, I get how I named the argument in the function, not which argument I passed to the function. – llrs Mar 12 '14 at 14:25
  • no prob, did you get it working? i would say just inspect the object in question as you need to. – acushner Mar 12 '14 at 14:48
  • actually, i think an easier way is to just grab the `locals` dictionary when you enter the function. make sure you make a copy. – acushner Mar 12 '14 at 14:52
  • 1
    Well, now I moved into another problem not related. As soon as I manage I mark your answer ;) – llrs Mar 12 '14 at 14:52