+ 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?

24th Oct 2018, 2:13 AM
LONGTIE👔
LONGTIE👔 - avatar
17 ответов
+ 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
25th Oct 2018, 11:27 AM
Flandre Scarlet
Flandre Scarlet - avatar
+ 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!
5th Dec 2018, 3:44 AM
Flandre Scarlet
Flandre Scarlet - avatar
+ 8
the Zen of Python: "There should be one-- and preferably only one --obvious way to do it."
24th Oct 2018, 4:32 AM
Mert Yazıcı
Mert Yazıcı - avatar
+ 6
def func(x = None, y = None): x = x or 5 y = y or 1 print(x*y) func()
24th Oct 2018, 5:09 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 6
Flandre Scarlet, Kishalaya Saha, why do you put * in front of your function variables or in front of your functions name?
4th Dec 2018, 9:10 PM
LONGTIE👔
LONGTIE👔 - avatar
+ 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
25th Oct 2018, 2:30 PM
Flandre Scarlet
Flandre Scarlet - avatar
+ 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
26th Oct 2018, 7:14 AM
David Ashton
David Ashton - avatar
+ 5
Kishalaya Saha I've also seen ** is that the same?
5th Dec 2018, 3:31 AM
LONGTIE👔
LONGTIE👔 - avatar
+ 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
24th Oct 2018, 5:51 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
Paul Jacobs it's python, there's usually more then one way to do something.
24th Oct 2018, 2:23 AM
LONGTIE👔
LONGTIE👔 - avatar
+ 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.
5th Dec 2018, 3:15 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 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.
24th Oct 2018, 3:15 AM
Maninder $ingh
Maninder $ingh - avatar
+ 2
Flandre Scarlet how do we make them immutable? i thought python did not have constants.
25th Oct 2018, 2:20 PM
LONGTIE👔
LONGTIE👔 - avatar
+ 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 😉
5th Dec 2018, 3:59 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
Why don't you try, it's quicker than asking.
24th Oct 2018, 2:20 AM
Paul
Paul - avatar
+ 1
values are setted. and can be changed.
24th Oct 2018, 4:48 PM
Alex[]
Alex[] - avatar
0
easy: in the paramaters put: example: x=4, y=6
25th Oct 2018, 2:30 PM
Alex[]
Alex[] - avatar