Order of default and non-default arguments?

Well range is C code which can do this slightly better. Anyways, you can do this.

Up vote 4 down vote favorite share g+ share fb share tw.

In Python, I understand that default arguments come at the end and that non-default arguments cannot follow a default argument. That is fine. Like for example: >>> def foo(x=0, y): return x, y SyntaxError: non-default argument follows default argument That is OK as expected.

However, what about the case when I want that the first argument should be a default one? Like for example, as is apparent from the above code, x has to be the first argument and it should have a default value of 0. Is it possible to do this?

I am asking because even in the range function, I am guessing it is something like this: def range(start=0, end): pass So how is this done and if it is not possible, how is this implemented by range? Note that I am insisting on the first argument to be default, that is the entire point. I am using range as an example because it fits my problem perfectly.

Of course one could implement range as def range(end, start=0), but that is not the point. Python argument-passing link|improve this question asked Nov 9 '10 at 19:43user2253129,71311447 93% accept rate.

Well, range is C code which can do this slightly better. Anyways, you can do this: def range(start, stop=None): if stop is None: # only one arg, treat stop as start ... start = 0 stop = start ... and document the function accordingly.

Nearly identical to my answer, but clearer and five minutes earlier. So, +1. – Steven Rumbalski Nov 9 '10 at 19:59 Might want to add a step argument for consistency with built-in.

– Steven Rumbalski Nov 9 '10 at 20:00 +1. That is perfect. I feel stupid not thinking of this.

– user225312 Nov 9 '10 at 20:05.

There are a couple approaches. The first would be to switch the arguments in the function, if some of the arguments are "None". That would work like this.

Def range1(value, end=None): if end == None: end = value value = 0 return _generate_range_values(value, end) The other primary method would be to have your function get a list of all arguments it receives. Then it can decide what to do, based on the number of arguments. Def range2(*args): if len(args) == 1: start = 0 end = int(args0) elif len(args) == 2: start = int(args0) end = int(args1) return _generate_range_values(start, end) The third would be to encourage users to pass named arguments to your function, which makes the order less important.

Def range3(end, start=0): return _generate_range_values(start, end) Then users would call it with the named start argument when they wanted something besides 0. (Although the named argument would not be required, it keeps the code clear. For I in range3(55, start=12).

1 Even though I don't understand why did you felt the need to use int() on argsx in range2() but not in the others. It's not necessary (although it does allow that version to accept floating point and string arguments like 3.14 and "42"). – martineau Dec 12 '10 at 12:42 Looking back, I'm not sure why I used int() in one of the examples and not others.

The builtin range() function will raise an error if it gets anything besides integer arguments. – Peter Shinners Jan 8 '11 at 17:25.

You can handle the Exceptions yourself if you really want that def Range(start=0, end=None): if end is None: raise AttributeError("end value not specified") pass.

It is not implemented by range. You can use *args or **args and treat the tuple or the dict as you want. For example: def f(*args): if len(args) == 1: print "assuming the first is default" elif len(args) == 2: print "two arguments were passed" else: print "Complaining.

I don't have the code for range, but I'm certain it performs this kind of trick: def range(start, stop=None, step=1): if stop is None: start, stop = 0, start ... edit: Code corrected per martineau's comment.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions