0

Is it possible to use f-string to modify output of Python's default argument? I want such functionallity to re-name one argument per function's calling.

When calling a function with xyz argument I would like to see it injected into v in f"this_is_{v}.

Below pseudo code to give high-level idea of what I'm aiming at.

def function(parameter=f"this_is_{v}"):
    print(parameter)

function("first")
#prints this_is_first

function("second")
#prints this_is_second

Was thinking about alternative mechanism for below code:

def function(v):
    value=f"this_is_{v}"
    print(value)

function("first")
function("second")
marcin2x4
  • 1,321
  • 2
  • 18
  • 44
  • No. But you can simply assign the variable *inside* the function instead of using a default parameter value. – Konrad Rudolph Mar 01 '23 at 13:13
  • What is the exact problem you are trying to solve? - to me it sounds like you have an [XY problem](https://xyproblem.info/) – matszwecja Mar 01 '23 at 13:13
  • 1
    It is possible to use f-strings in default arguments out of the box. What is not possible, is having default arguments depend on other arguments, especially since default args are evaluated **only on function definition**. They are created way before your `v` would get any chance to have an actual value. – matszwecja Mar 01 '23 at 13:18
  • @matszwecja - I want to pass argument `first` into `v` when calling `function()`. Does it help now? – marcin2x4 Mar 01 '23 at 13:59
  • So... just `def function(v)` and `function("first")`? Could you clarify what you mean by "I want such functionallity to re-name one argument per function's calling." – matszwecja Mar 01 '23 at 14:00
  • Nevermind, I found solution that works fine. I added working solution to my question. – marcin2x4 Mar 01 '23 at 14:03
  • 1
    This was a fine question before you edited it. If you have your own solution, add it as an answer to your question. – Craig Mar 01 '23 at 14:14

3 Answers3

1

No, using argument values in the default value of another parameter (either via f-strings or any other means) is not supported. But you can do the following, which is closely equivalent to what you are trying to achieve:

def function(v, parameter=None):
    if parameter is None:
        parameter = f"this_is_{v}"
    print(parameter)
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Answering the actual question, yes it is possible to have f-strings in default arguments. Problem lies elsewhere and the question could use some more clarification. – matszwecja Mar 01 '23 at 13:20
  • @matszwecja OP's question clearly states that they need f-strings *referencing other parameters*, which is not possible. My answer provides a solution to this specific issue. This is also a fairly common conceptual problem, and does not a priori require further clarification. It's a clear-cut question and answer. – Konrad Rudolph Mar 01 '23 at 13:26
  • 2
    Maybe [eventually](https://peps.python.org/pep-0671/) it will be possible... – Kelly Bundy Mar 01 '23 at 13:34
  • @KellyBundy I'm not sure how I feel about this one, on one hand it looks handy, on the other it could overcomplicate function signatures with long expressions where they should be short and concise. – matszwecja Mar 01 '23 at 13:42
  • @matszwecja Well, nobody would force you to abuse the feature :-). Use it in good ways, not in bad ways. – Kelly Bundy Mar 01 '23 at 13:52
  • I added alternative code in my question that works just fine ;) – marcin2x4 Mar 01 '23 at 14:06
1

Answering the question, yes it is possible. No special syntax needed.

s = 123

def function(parameter=f"this_is_{s}"):
    print(parameter)
function() # this_is_123

Your problem lies elsewhere and probably comes from misunderstanding default arguments.

Default arguments are evaluated only once, when the function is defined. It is at this time all the variables that are evaulated for default parameter must be defined with some value. We can easily verify that by changing s value to something else and calling function again:

s = 456
function() # still prints this_is_123

This is because our parameter was already evaluated to string "this_is_123" and will stay like that unless we redefine function.

This also means you cannot use value of v for parameter, because such value only exists when we are calling the function. This is after we decided default values.

Related: "Least Astonishment" and the Mutable Default Argument

To finish - your questions makes it sound like you are trying to solve a different problem than described, and if you edit your question to describe more clearly what is your goal, you could get much more helpful answers.

matszwecja
  • 6,357
  • 2
  • 10
  • 17
  • My question was about mechanism that would by-pass your explanation. So it would work that value assignment `first into v` happend when calling `function(param=f"this_is_v")` – marcin2x4 Mar 01 '23 at 14:02
0

Working solution that required moving f-string inside function's body:

def function(v):
    value=f"this_is_{v}"
    print(value)

function("first") #this_is_first
function("second") #this_is.....
marcin2x4
  • 1,321
  • 2
  • 18
  • 44