+ 1

How to make a function which can take second required argument ?

In range(start, stop, step) we can directly pass `stop` argument because start, and step are optional if we pass start and stop both i.e. range(start, stop) then start is the first positional argument and stop is at second place. I want to make this type of function where i can pass second argument directly. I try to create it but if I make start optional then I have to make it keyword arg and set it to None and No positional arg can follow kwarg. How can I do it?

29th Apr 2021, 5:00 PM
KUMAR SHANU
KUMAR SHANU - avatar
8 odpowiedzi
+ 1
python is different than other languages , it require to use the optional arguments after the required arguments . so this will work hopefully def fn(req1, req2, opt1="A", opt2="B") Hope this what u're lookin for
29th Apr 2021, 5:14 PM
Med Amine Fh
Med Amine Fh - avatar
0
You can do This : def fn(opt="default", req, opt2="default2")
29th Apr 2021, 5:03 PM
Med Amine Fh
Med Amine Fh - avatar
0
Med Amine Fh this is wrong in python's syntax
29th Apr 2021, 5:06 PM
KUMAR SHANU
KUMAR SHANU - avatar
0
Med Amine Fh I have read those questions but My question is different from these. I know how to use optional args in python. But it is not working in that way which I want it to work
29th Apr 2021, 5:08 PM
KUMAR SHANU
KUMAR SHANU - avatar
0
I know that, but python also has range(start, stop, step) function where start is optional, stop is required and step is also optional. So how they have put one positional arg in between two optional args. How can I achieve that same thing
29th Apr 2021, 5:16 PM
KUMAR SHANU
KUMAR SHANU - avatar
0
Yes, you are right Mirielle that may be possible, but overloading also not possible in python. If I write two functions with the same name then the latest one always overwrite the later one. So, how to achieve overloading
30th Apr 2021, 4:14 AM
KUMAR SHANU
KUMAR SHANU - avatar