Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: can i set a variable to equal a function of itself?

Tags:

python

Can I do this?

var1 = some_function(var1)

When I tried to do this I got errors, but perhaps I was doing something wrong.

like image 885
Alex Gordon Avatar asked Jul 02 '26 06:07

Alex Gordon


1 Answers

If the variable has been previously defined, you can do that yes.

Example:

def f(x):
    return x+1

var1 = 5
var1 = f(var1)
# var1 is now 6

If the variable has not been defined previously, you can't do that, simply because there is no value that could be passed to the function.

like image 75
sepp2k Avatar answered Jul 03 '26 18:07

sepp2k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!