0

Is it possible to make a function's keyword argument default defined to a variable name? I want to do this because I'm finding myself creating functions with a lot of keyword arguments, but I'd like these keyword arguments default to a variable name I define before using the function. I'm also open to solutions that allows me to fix the repetitiveness problem in other ways. this was just one idea i was musing about.

for instance, i'd like to do something like this

def do_something(x=x, y=y, z=z, t=t):
    return x, y

I know the above returns an error because I haven't defined x yet.

i'm too lazy to constantly write:

x = 100
y= 50
z= 10
t= 1

do_something(x=x, y=y, z=z, t=t)

I'd rather just write with the defaults already assumed as the variable names:

x = 100
y= 50
z= 10
t= 1

do something()
user3314418
  • 2,903
  • 9
  • 33
  • 55
  • what is wrong with your last example? – Padraic Cunningham May 13 '14 at 00:35
  • What's wrong with `do_something(x=100, y=50, z=10, t=1)`? Or, heck, `do_something(100, 50, 10, 1)`? The thing you're too lazy to write doesn't seem to be something you actually have to write. – user2357112 May 13 '14 at 00:36
  • 1
    If you do this, the behaviour of your function will depend on whatever state those global variables happen to be in at the time you call your function. If those variables change and you forget to take account of that when calling the function, you'll get the wrong output, which is why people generally don't recommend it. – Marius May 13 '14 at 00:36
  • @Marius, that's not true. the values are fixed when the function is defined – linuts May 13 '14 at 00:44
  • @linuts: Yeah, sorry. To be clear, you can write a function that behaves in the same way as OP's last example/desired behaviour, but it will be subject to the problem I described. – Marius May 13 '14 at 00:46
  • Does this answer your question? [Passing keyword arguments to a function when local variable names are same as function parameter names](https://stackoverflow.com/questions/5860974/passing-keyword-arguments-to-a-function-when-local-variable-names-are-same-as-fu) – Mihai Capotă Aug 11 '23 at 22:23

3 Answers3

1

Python lets you pass in a dictionary and tell the function to unpack it

args = dict(x=100, y=50, z=5, t=1)
def do_something(args) # star tells the function to unpack as dict the variables you made
do_something(**args) # call it like this

You can even pass it in as a list with one star:

args = [100, 50, 5, 1]
def do_something(x,y,z,t) # star tells the function to unpack the variables you made
do_something(*args) # call it like this

Python also allows default parameters:

do_something(x=100, y=50, z=5, t=1) #if you don't pass arguments, it will use these defaults
Roy Iacob
  • 412
  • 3
  • 13
1

The easiest way is to use a dictionary instead of variables:

def do_something(x, y, z, t):
    ...

data = dict(
    x = 100,
    y = 50,
    z = 10,
    t = 1
)

do_something(**data)

Here's a really hacky way to do it:

import inspect

def autofill(fn):
    def _autofill(*args, **kwargs):
        kwargs.update({arg:globals()[arg] for arg in inspect.getargspec(fn).args if arg not in kwargs})
        return fn(*args, **kwargs)
    return _autofill

>>> @autofill
... def f(x,y):
...     return x,y
...
>>> x = 1; y = 20
>>> f()
(1, 20)
>>> f(y=2)
(1, 2)
Matthew Trevor
  • 14,354
  • 6
  • 37
  • 50
1

Have you checked the functools module?

from functools import partial

def do_something(x,y,z,t):
print(x,y,z,t)

do_something = partial(do_something, x=100, y=50, z=10, t=1)

do_something()
do_something(x=30)
chapelo
  • 2,519
  • 13
  • 19
  • oo this is interesting too. i haven't heard of this. for now, i'll stick with a dictionary for simplicity, but i may try this in the future. – user3314418 May 13 '14 at 00:46