Getting the argument names passed to a function ?

Quinn Dunkan quinn at riyal.ugcs.caltech.edu
Wed Apr 5 02:27:28 CEST 2000


On Tue, 04 Apr 2000 21:08:55 GMT, Fredrik Lundh <effbot at telia.com> wrote:
>Andreas Jung <ajung at sz-sb.de> wrote:
>>
>> def myfunc(arg1,arg2):
>> ...
>>
>>
>> a=1;b=2
>> myfunc(a,b)
>>
>> Is it possible to get the name of variables that  were
>> used as arguments to call myfunc(). myfunc() should be
>> able to determine the names 'a' and 'b'. Don't ask
>> me why I need this - it's for a worse Python hack :-)
>
>short answer: no
>
>longer answer: maybe, using the incredibly clever and
>totally evil bytecodehacks stuff:
>
>    http://bytecodehacks.sourceforge.net

Hmm, could you say:

def arghack(arg):
    for k, v in globals().items():
        if arg is v:
            return k
    else:
        return None

Seems to work ok...



More information about the Python-list mailing list