Doubt with python function. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Doubt with python function.

I read in the book "byte of python" and also saw an error coz of putting default argument before a non-default argument in a function. Like, def say(x='hello',y): print(x*y) Is there any reason for this specic way of writing parameters with default values or is it just made like that?

17th Sep 2021, 10:05 AM
partha
partha - avatar
7 Answers
+ 2
Nah, it's a legit question. I just wanted to know if it was something you actually were looking into or if it's just a random question. That's how python is set up. It's got a reason, just not one thats easily explained. Nothing in code is just there for no reason.
17th Sep 2021, 11:00 AM
Slick
Slick - avatar
+ 1
Ty mate!
17th Sep 2021, 11:23 AM
partha
partha - avatar
0
You could just set y to something default too, even if it's None. You could also, put x second as a parameter instead of y. When you tried it, it gave you an error right? partha what? Why?
17th Sep 2021, 10:15 AM
Slick
Slick - avatar
0
yes, it gave an error but what I want to ask is, is there any reason to this way of writing parameters with default values? (i.e. Only those parameters which are at the end of the parameter list can be given default argument values)
17th Sep 2021, 10:23 AM
partha
partha - avatar
0
That's what was written in the book "byte of python", I also tried it out and it gives this error " SyntaxError: non-default argument follows default argument". I'm asking why??why is it like that? Slick Don't why me lmao >.>
17th Sep 2021, 10:33 AM
partha
partha - avatar
0
Nah, my bad, and I mean no disrespect but I have to ask. It just makes no sense to me why someone would ask that. So if you can tell me why, like explain what you're trying to do, it can shed light on why you'd ask. Its like asking why different countries drive on different sides of the road. There's obviously a reason, but why should it even be a question?
17th Sep 2021, 10:38 AM
Slick
Slick - avatar
0
Ah sorry if I sounded too confusing, I was simply intrigued if it had something to do with HOW functions work in python. It's fine if there is no specific reason but I would love to know if it has. Is my question too silly? (>0<;)
17th Sep 2021, 10:52 AM
partha
partha - avatar