Changing function's default parameters in Python [duplicate] - python

This question already has answers here:
Is it possible to change a function's default parameters in Python?
(4 answers)
Closed 6 years ago.
Let's say this is the signature of my function:
def foo(x,y,z=0):
.
.
When I want to use this function, how can I override the value of z without changing the function or the signature?

Just pass value explicitly.
def foo(x, y, z=0):
print z
foo(1,3)
>> 0 # default value
foo(1,2,5)
>> 5 # new value passed

Related

Can you directly alter the value of a non-list argument passed to a function? [duplicate]

This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 13 days ago.
This snippet of python code
def changeValue(x):
x = 2*x
a = [1]
changeValue(a[0])
print(a[0])
Will print the result "1" when I would like it to print the result "2". Can this be done by only altering the function and no other part of the code? I know there are solutions like
def changeValue(x):
x[0] = 2*x[0]
a = [1]
changeValue(a)
or
def changeValue(x):
return 2*x
a = [1]
a[0] = changeValue(a[0])
I am wondering if the argument passed as-is can be treated as a pointer in some sense.
[edit] - Just found a relevant question here so this is likely a duplicate that can be closed.
No it's not possible. If you pass a[0], it's an int and it can't be mutated in any way.
If the int was directly in the global namespace, you could use global keyword. But it's not. So again no.

Is there a closure/execution context (like js) in Python? How does python manage those scenarios? Why's function assuming values instead defaults [duplicate]

This question already has answers here:
Why does using `arg=None` fix Python's mutable default argument issue?
(5 answers)
"Least Astonishment" and the Mutable Default Argument
(33 answers)
Closed 10 months ago.
For a DP problem, I am using typical memoization using python dictionaries. In the below code, why is memo assuming values when nothing is passed?
Passing by reference understates only the memo is passed by address and not value, but what if there's no 'passing', shouldn't the default override?
my simplified code:
## simple recursive function that adds y recursively
def recurFunc(x,y, memo = {}):
print('Called with x: {}, memo: {}'.format(x,memo))
if x == 1: return y
if x in memo: return memo[x]
memo[x] = y+ recurFunc(x-1,y, memo)
return memo[x]
print(recurFunc(5,1))
#above statement returns 5 as expected
print(recurFunc(6,2))
#above statement returns 7 instead of 12; this is because the memo has older values
A workaround to solve this problem is simple; but want to know what's happening under the hood.

I want print paramater's name in a python function [duplicate]

This question already has answers here:
How to print actual name of variable class type in function?
(4 answers)
Getting the name of a variable as a string
(32 answers)
Closed 3 years ago.
I want print paramater's name in a python function
First, a variable is assigned a value.Then, pass the variable to a function.
Last,I want to print str(variable) inner the function.
varibale = 1234
def f(x):
print(....)
return
f(varibale)
Expected output is print out the 'varibale' whatever variable is.
if a = 1 ==> f(x), expected output is 'a';if b = 2 ==> f(x), expected output is 'b'.....
Why do you want to print out the value using a function?
If you know the name of the variable, you can simply write your requested function output yourself by just putting 'variable'
So in your example: instead of using f(x), just use print('x')

python logical operator with variable number of arguments [duplicate]

This question already has answers here:
How do Python's any and all functions work?
(10 answers)
Closed 5 years ago.
is there a way in python to check the logical operator AND with variable number of arguments. for example:
def and_function(a,b,c,d,e....):
if a and b and c and d and c and d and e and . and . is True:
return True
i see that this is possible using *args function and using a counter. But wish to know if there is a better way of doing it.
You basically want all() but to customize it you can use this:
def and_function(*args):
return all([a > 5 for a in args])

Call a function inside another function call [duplicate]

This question already has answers here:
Expanding tuples into arguments
(5 answers)
Closed 6 years ago.
def func1(arg1,arg2,arg3,arg4)
...
def func2(arg5,arg6)
return a,b,c,d
func1(func2(arg5,arg6))
Can I call func1(func2(arg5,arg6)) like this??
as func2 will return 4 items that'll be passed to func1
You would have to unpack the arguments, but yes you can do that using the * operator.
func1(*func2(arg5,arg6))

Categories