+ 5
[SOLVED] How to set a default value in a function?
https://code.sololearn.com/cQZkZG4ZB152/?ref=app i tried this, but is this really the only way?
17 Answers
+ 6
default values should be immutable, otherwise it may change
for example,
def func(a=[], b=1):
a.append(b)
return a
print(*func()) # 1
print(*func()) # 1 1
https://code.sololearn.com/c4Nyk8BMcFIR
+ 9
LONGTIE👔👍 that's for a set of keyword-values
For example,
args = [1,2,3]
kwargs = {sep:"/", end:"!"}
print(*args, **kwargs) #1/2/3!
+ 8
the Zen of Python:
"There should be one-- and preferably only one --obvious way to do it."
+ 6
def func(x = None, y = None):
x = x or 5
y = y or 1
print(x*y)
func()
+ 6
Flandre Scarlet, Kishalaya Saha, why do you put * in front of your function variables or in front of your functions name?
+ 5
LONGTIE👔 some types are already immutable, tuple, int, None for example, that's why we can give default as int without any problem
for the mutable types, the better (or maybe only) method is like what Kartikey Sahu or Kishalaya Saha does
+ 5
Another way around Flandre Scarlet 's anomaly is
def func(a=None, b=1):
if a is None:
a=[]
a.append(b)
return a
+ 5
Kishalaya Saha I've also seen **
is that the same?
+ 4
I agree with Mert. This is the best and also the recommended approach. If you showed an example where this doesn't work so well, then it would be easier for us to find a way to tackle that issue. Either way, here's a silly alternative that probably works in all cases.
def func(*args):
if len(args) == 2:
x, y = args
elif len(args) == 1:
x, y = *args, 1
else:
x, y = 5, 1
print(x*y)
func()
I won't recommend it myself :D
+ 3
Paul Jacobs it's python, there's usually more then one way to do something.
+ 3
Okay, I'll get you started, but you should experiment more with them. Pretty cool stuff!
"def func(*args):" allows me to call the function with as many arguments as I like, or even no arguments at all. args becomes a tuple containing those arguments. So func(), func(1), func(1, 2, 3) are all valid. If I didn't use the * in the definition, I would have had to do func((1, 2, 3)) to get the same effect.
When I do "x, y = *args, 1", basically the reverse happens. It "expands" or "opens the container of" args. So if args here was the singleton tuple (7,), then it is read as x, y = 7, 1. Contrast this with x, y = args, 1, which would make x the tuple (7,) as opposed to the int 7.
It's similar with Flandre's case. If we have a list a=[1, 2, 3], print(a) outputs "[1, 2, 3]", but print(*a) outputs "1 2 3". You can even do print(*range(5)), which won't print the range object, but the values it produces. Flandre used print(*func()), which just executes func(), and uses * on the list it returns.
+ 2
I think in python we can provide default value to function with the help of = .i think there is no other method for to do this.
+ 2
Flandre Scarlet how do we make them immutable?
i thought python did not have constants.
+ 2
In addition to what Flandre said, you can also use it in the reverse fashion for a function definition, just like *args.
So "def func(**kwargs):" makes kwargs a dictionary that accepts named (keyword) arguments. So we can call the function like func(x=2, y=3), and kwargs becomes the dictionary {x: 2, y: 3}.
I suggest you review the "Pythinicness and Packaging" chapter of the Python tutorial on Sololearn. It doesn't cover everything though, like what Flandre just did with her cool example 😉
+ 1
Why don't you try, it's quicker than asking.
+ 1
values are setted. and can be changed.
0
easy: in the paramaters put: example: x=4, y=6