0

I want to make some function like below:

def test_fn(var1: int = 1, var2: int = var1+1):     
    return var1, var2

(and the function above simply does not work which would raise NameError)

Is there any smart way of referencing var2 from var1 as input variable without adding or running other lines before this function?

Thanks in advance :D

4
  • 2
    You would have to put that logic inside the body of the function. Commented Jun 27, 2023 at 2:11
  • @larsks ah, I wanted to avoid that way, but I guess I have to stick with that lol xD Thx! Commented Jun 27, 2023 at 2:13
  • 1
    Python calculates default values when the function is defined, not when it's called. So this is impossible. Commented Jun 27, 2023 at 2:16
  • 1
    This is proposed in PEP 671 which isn't accepted or rejected yet. Maybe it'll be possible in the future. Commented Jun 27, 2023 at 2:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.