Mailing List Archive

Introspecting the variable bound to a function argument
Hello, all.

Does Python have an instrospection facility that can
determine to which outer variable a function argument is
bound, e.g.:

v1 = 5;
v2 = 5;

def f(a):
print(black_magic(a)) # or black_magic('a')

f(v1) # prints: v1
f(v2) # prints: v2

--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
--
https://mail.python.org/mailman/listinfo/python-list
Re: Introspecting the variable bound to a function argument [ In reply to ]
?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@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

--
https://mail.python.org/mailman/listinfo/python-list
Re: Introspecting the variable bound to a function argument [ In reply to ]
On Wednesday, February 22, 2023 at 2:32:57 AM UTC-8, Anton Shepelev wrote:
> Hello, all.
>
> Does Python have an instrospection facility that can
> determine to which outer variable a function argument is
> bound, e.g.:
>
> v1 = 5;
> v2 = 5;


do some Python coders like to end lines with ; ?


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

the term [call by name] suggests this should be possible.


30 years ago... i used to think about this type of thing A LOT ---
------- CBR, CBV, CBN, (call by value), (call by name).... etc.

--
https://mail.python.org/mailman/listinfo/python-list
Re: Introspecting the variable bound to a function argument [ In reply to ]
On 2/22/2023 3:12 PM, Hen Hanna wrote:
> On Wednesday, February 22, 2023 at 2:32:57 AM UTC-8, Anton Shepelev wrote:
>> Hello, all.
>>
>> Does Python have an instrospection facility that can
>> determine to which outer variable a function argument is
>> bound, e.g.:
>>
>> v1 = 5;
>> v2 = 5;
>
>
> do some Python coders like to end lines with ; ?

Very few, probably. It's not harmful but adds unnecessary visual clutter.

>>
>> def f(a):
>> print(black_magic(a)) # or black_magic('a')
>>
>> f(v1) # prints: v1
>> f(v2) # prints: v2
>>
>
> the term [call by name] suggests this should be possible.
>
>
> 30 years ago... i used to think about this type of thing A LOT ---
> ------- CBR, CBV, CBN, (call by value), (call by name).... etc.
>

--
https://mail.python.org/mailman/listinfo/python-list
Re: Introspecting the variable bound to a function argument [ In reply to ]
On 23/02/23 9:12 am, Hen Hanna wrote:
> On Wednesday, February 22, 2023 at 2:32:57 AM UTC-8, Anton Shepelev wrote:
>> def f(a):
>> print(black_magic(a)) # or black_magic('a')
>>
>> f(v1) # prints: v1
>> f(v2) # prints: v2
>>
>
> the term [call by name] suggests this should be possible.

But Python doesn't use call-by-name or anything remotely like it.

(Even if it did, the word "name" in that context doesn't mean
what it sounds like it means. The Algol docs used some words in
weird ways.)

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list