+ 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?
7 Réponses
+ 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
0
You can do This :
def fn(opt="default", req, opt2="default2")
0
Med Amine Fh this is wrong in python's syntax
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
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