Introspecting the variable bound to a function argument

Barry barry at barrys-emacs.org
Wed Feb 22 12:21:49 EST 2023


please do not use an email address on a public list that cannot be replied to.


> On 22 Feb 2023, at 14:43, Anton Shepelev <anton.txt at g{oogle}mail.com> wrote:
> 
> Hello, all.
> 
> Does Python have an instrospection facility that can
> determine to which outer variable a function argument is
> bound, e.g.:

There is no requirement for a variable to be used in the call.
It could be an an int or string, 42 “forty two”.

> 
>  v1 = 5;
>  v2 = 5;
> 
>  def f(a):
>      print(black_magic(a)) # or black_magic('a')
> 
>  f(v1) # prints: v1
>  f(v2) # prints: v2

There is the traceback module that lets you find where you are called from.
Also there is the inspect module that also lets tou get at the stack in more detail.

Barry

> 
> -- 
> ()  ascii ribbon campaign -- against html e-mail
> /\  www.asciiribbon.org   -- against proprietary attachments
> -- 
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list