Shorten Keyword Arguments with Implicit Notation: foo(a=a, b=b) to foo(.a, .b)

Not sure if this has been discussed here before, but this is the idea.

We all know that when calling a function, if you have a variable that’s named the same as one of the function’s arguments, you have to write it out fully. Like this:

def my_function(a, b):
    pass

a = 1
b = 2

my_function(a=a, b=b)

When there are a lot arguments and argument names are long, this can be very verbose. What if we could make this shorter and just write:

my_function(.a, .b)
my_function(a=a, b=b) # these two lines do the same thing

This idea is actually from Systemverilog. I use the feature all the time. Hope python can have a similar feature. It can apparently make a lot of function calls with keyword arguments a lot shorter and cleaner.

There is no such limitation. The variable a and b will be different than the global a and b as they are scoped differently. Python will not be confused by this and it will run perfectly fine.

In the example below, we can see that we can pass global a and b, manipulate the content in the function, print it, and the global a and b will not be affected.

def my_function(a, b):
    a += 1
    b += 1
    print(a, b)

a = 1
b = 2

my_function(a, b)
print(a, b)

Output

2 3
1 2

We’ve seen the idea as recently as a few weeks ago:

yeah, I understand the difference here. Thanks. What I wanted to propose was just a syntax improvement.

Thanks. Great to know there is already long discussion! Should I delete this post?

Ah, I just misinterpreted, the intent, no worries.

Duplicate of Allow identifiers as keyword arguments at function call site (extension of PEP 3102?)