Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why python doesn't use syntax like function(,,x) for default parameters?

Reply
Thread Tools

Why python doesn't use syntax like function(,,x) for default parameters?

 
 
Dmitry Anikin
Guest
Posts: n/a
 
      03-10-2006
I mean, it's very convenient when default parameters
can be in any position, like
def a_func(x = 2, y = 1, z):
...
(that defaults must go last is really a C++ quirk which
is needed for overload resolution, isn't it?)

and when calling, just omit parameter when you want to
use defaults:
a_func(, , 3)

There are often situations when a function has independent
parameters, all having reasonable defaults, and I want to
provide just several of them. In fact, I can do it using
keyword parameters, but it's rather long and you have to
remember/lookup names of parameters.

Is there some contradiction in python syntax which disallows
an easy implementation of this feature, or just nobody bothered
with this? If former is the case, please show me why, because
I badly need this feature in embedded python app (for
compatibility with other language that uses such syntax) and might
venture to implement it myself, so don't want to waste time
if it's gonna break something.
Or maybe it might be an idea for enhancement proposal?

 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      03-10-2006
Dmitry Anikin wrote:

> Is there some contradiction in python syntax which disallows
> an easy implementation of this feature, or just nobody bothered
> with this? If former is the case, please show me why, because
> I badly need this feature in embedded python app (for
> compatibility with other language that uses such syntax) and might
> venture to implement it myself, so don't want to waste time
> if it's gonna break something.
>

I think you would find it hard to implement exactly that in Python. Of
course what you can do easily enough is invent a magic 'missing' value and
map Python calls of:

a_func(missing, missing, 3)

to a call of:

a_func(,,3)

in your other language. It seems to me though that writing:

a_func(z=3)

ought to be clearer to anyone reading the code, especially when you come
back to your code in 6 months time and find:

b_func(,,,,,,,,,,,1,,,,2)


Remember Python is a dynamic language, so the compiler cannot tell which
function you are actually calling. In a statically bound language parameter
defaults are often restricted to constant values and the default values are
simply inserted in place of any missing arguments when the call is
compiled. That isn't possible in Python, so the interpreter has to insert
the missing values when you actually make the call. The code to do that is
complex enough with the current restrictions.

To allow arbitrary arguments to be defaulted, I think you would have to add
a new magic value to represent a missing parameter, make the compiler pass
that in place of any omitted positional parameters, and then make the
function call code check all parameters to see if they are missing values
and if so substitute the actual default. It would be possible, but it
doesn't really give you any additional power since you can already do that
in the cases where you need it by using keyword arguments or by passing an
explicit value to mean 'replace this parameter by some other canned value'.
 
Reply With Quote
 
 
 
 
Terry Hancock
Guest
Posts: n/a
 
      03-10-2006
On 10 Mar 2006 09:51:01 GMT
Duncan Booth <(E-Mail Removed)> wrote:
> Dmitry Anikin wrote:
> > Is there some contradiction in python syntax which
> > disallows an easy implementation of this feature, or
> > just nobody bothered with this? If former is the case,
> > please show me why, because I badly need this feature in
> > embedded python app (for compatibility with other
> > language that uses such syntax) and might venture to
> > implement it myself, so don't want to waste time if it's
> > gonna break something.
> >

> I think you would find it hard to implement exactly that
> in Python. Of course what you can do easily enough is
> invent a magic 'missing' value and map Python calls of:
>
> a_func(missing, missing, 3)
>
> to a call of:
>
> a_func(,,3)


It's not uncommon, of course, to use "None" for this
purpose. I have a lot of code that does something like:

def myfunc(a, b, c):
if a is None:
a = []
...


--
Terry Hancock ((E-Mail Removed))
Anansi Spaceworks http://www.AnansiSpaceworks.com

 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      03-10-2006
"Dmitry Anikin" <anikin#remove_this#@vstu.ru> wrote:
> There are often situations when a function has independent
> parameters, all having reasonable defaults, and I want to
> provide just several of them. In fact, I can do it using
> keyword parameters, but it's rather long and you have to
> remember/lookup names of parameters.


Specifying the names of the keyword parameters costs you a little typing
once, but saves everybody (including yourself) a lot of grief later when
you're trying to figure out what the heck your code does 6 months later.

> I badly need this feature in embedded python app (for
> compatibility with other language that uses such syntax)


Can you tell us more about what it is that you're trying to do?

> Or maybe it might be an idea for enhancement proposal?


You can always write up a PEP, but to be honest, this doesn't sound like
one that would meet with much enthusiasm from the community.
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      03-10-2006
Dmitry Anikin wrote:
> I mean, it's very convenient when default parameters
> can be in any position, like
> def a_func(x = 2, y = 1, z):
> ...
> (that defaults must go last is really a C++ quirk which
> is needed for overload resolution, isn't it?)
>

I've no idea why C++ required defaults last; it certainly seems wise to
avoid confusion with the positionals in Python.

> and when calling, just omit parameter when you want to
> use defaults:
> a_func(, , 3)
>

Yerch! So now you've forced all arguments to be positional? This doesn't
seem like an improvement. And it's just plain fugly.

> There are often situations when a function has independent
> parameters, all having reasonable defaults, and I want to
> provide just several of them. In fact, I can do it using
> keyword parameters, but it's rather long and you have to
> remember/lookup names of parameters.
>

Whereas you can invariably remember their positions? I don't think so.

> Is there some contradiction in python syntax which disallows
> an easy implementation of this feature, or just nobody bothered
> with this? If former is the case, please show me why, because
> I badly need this feature in embedded python app (for
> compatibility with other language that uses such syntax) and might
> venture to implement it myself, so don't want to waste time
> if it's gonna break something.
> Or maybe it might be an idea for enhancement proposal?
>

The thing about enhancement proposals is that they are supposed to
*improve* the language. Frankly I wouldn't see this as any kind of
enhancement.

If you have a large program to translate from another language you will
probably find that a modest application of Python suffices to translate
all the calls into whatever form turns out to be required in Python.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd www.holdenweb.com
Love me, love my blog holdenweb.blogspot.com

 
Reply With Quote
 
Antoon Pardon
Guest
Posts: n/a
 
      03-10-2006
Op 2006-03-10, Roy Smith schreef <(E-Mail Removed)>:
> "Dmitry Anikin" <anikin#remove_this#@vstu.ru> wrote:
>> There are often situations when a function has independent
>> parameters, all having reasonable defaults, and I want to
>> provide just several of them. In fact, I can do it using
>> keyword parameters, but it's rather long and you have to
>> remember/lookup names of parameters.

>
> Specifying the names of the keyword parameters costs you a little typing
> once, but saves everybody (including yourself) a lot of grief later when
> you're trying to figure out what the heck your code does 6 months later.


Could you explain what is so hard in figuring out:

func(,,4)

We sure don't seem to have a problem with figuring out things like

lst[::2]


Personnaly in a situation where it is likely that the first
parameter is going to take a default and the second parameter
is going to vary a lot, I would have prefered that to be
visible in how the function is called, instead of a call
with only one argument being interpreted as being the value
for the second parameter.

More specifically I would have preferred the possibility
of range(,n) and this being equivallent to range(0,n)
instead of range(n) being equivallent to range(0,n).

--
Antoon Pardon
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      03-10-2006
Antoon Pardon wrote:

>>
>> Specifying the names of the keyword parameters costs you a little typing
>> once, but saves everybody (including yourself) a lot of grief later when
>> you're trying to figure out what the heck your code does 6 months later.

>
> Could you explain what is so hard in figuring out:
>
> func(,,4)
>
> We sure don't seem to have a problem with figuring out things like
>
> lst[::2]


That is the usual polemics. Its a HUGE difference if I'm supposed to
remember 2 default values that are 0 and <size-of-sequence>, in a
specialized syntax, than arbitrary values

f(,,,,,3)

in some arbitrary function.

Diez
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      03-10-2006
In article <(E-Mail Removed)>,
Antoon Pardon <(E-Mail Removed)> wrote:

> Op 2006-03-10, Roy Smith schreef <(E-Mail Removed)>:
> > "Dmitry Anikin" <anikin#remove_this#@vstu.ru> wrote:
> >> There are often situations when a function has independent
> >> parameters, all having reasonable defaults, and I want to
> >> provide just several of them. In fact, I can do it using
> >> keyword parameters, but it's rather long and you have to
> >> remember/lookup names of parameters.

> >
> > Specifying the names of the keyword parameters costs you a little typing
> > once, but saves everybody (including yourself) a lot of grief later when
> > you're trying to figure out what the heck your code does 6 months later.

>
> Could you explain what is so hard in figuring out:
>
> func(,,4)


Because while I probably remember what func does (especially if it's well
named), it's less likely that I remember all the arguments it takes, and
even less that I remember what order they come in.

Let's say I've got a function which makes a network connection. It takes
optional arguments for a port number to connect to, a timeout (in seconds)
and a buffer size (in kbytes) to use. If we used your syntax, what does
"connect (,,20)" mean? You have to go look up the definition of the
function to find out, don't you? But, if I wrote "connect (port=20)", it's
obvious to anybody reading the code what the 20 is.
 
Reply With Quote
 
Juho Schultz
Guest
Posts: n/a
 
      03-10-2006
Antoon Pardon wrote:
> Op 2006-03-10, Roy Smith schreef <(E-Mail Removed)>:
>
>>"Dmitry Anikin" <anikin#remove_this#@vstu.ru> wrote:
>>
>>>There are often situations when a function has independent
>>>parameters, all having reasonable defaults, and I want to
>>>provide just several of them. In fact, I can do it using
>>>keyword parameters, but it's rather long and you have to
>>>remember/lookup names of parameters.

>>
>>Specifying the names of the keyword parameters costs you a little typing
>>once, but saves everybody (including yourself) a lot of grief later when
>>you're trying to figure out what the heck your code does 6 months later.

>
>
> Could you explain what is so hard in figuring out:
>
> func(,,4)
>


Your func has only three parameters, and only one non-default.
I think "all have reasonable defaults, I want to provide several"
means you might end up with bugs like this.

func(,,,1.2e-3,7.6e18,3.124576,3567.0,)
func(,,1.24e3,1,21.26e4,,,1220,57,35,0) # bug
TypeError: func() takes exactly 8 arguments (9 given)

Now what are the correct arguments?
1220.57, 35, and 0
1220, 57.35, and 0
1220, 57, and 35.0

With keywords parameters, this is easy to answer.

func(y=1.2e-3, z=7.6e18, i=3.124576, j=3567.0)
func(x=1.24e3, y=1, z=21.26e4, j=1220, k=57,35, w=0) # bug

SyntaxError: non-keyword arg after keyword arg.
 
Reply With Quote
 
Antoon Pardon
Guest
Posts: n/a
 
      03-10-2006
Op 2006-03-10, Diez B. Roggisch schreef <(E-Mail Removed)>:
> Antoon Pardon wrote:
>
>>>
>>> Specifying the names of the keyword parameters costs you a little typing
>>> once, but saves everybody (including yourself) a lot of grief later when
>>> you're trying to figure out what the heck your code does 6 months later.

>>
>> Could you explain what is so hard in figuring out:
>>
>> func(,,4)
>>
>> We sure don't seem to have a problem with figuring out things like
>>
>> lst[::2]

>
> That is the usual polemics. Its a HUGE difference if I'm supposed to
> remember 2 default values that are 0 and <size-of-sequence>, in a
> specialized syntax,


Those default values are not 0 and <size-of-sequence>, you may have
only experience with situations where they behave as such but that
is not the same.

> than arbitrary values
> f(,,,,,3)
>
> in some arbitrary function.


If you need to know these values then you will need to know them
just as much when a keyword is used or when the default values
are used later. Calling

f(3) or f(arg5=3)

Will give you no more a clue about the missing default values
than calling

f(,,,,,3)

At least in the last call you are given a clue about missing
arguments.

--
Antoon Pardon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax error? What syntax error? Assignment fo default values? Mark Richards Perl Misc 3 11-18-2007 05:01 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
Why is Default.aspx not present in the Default Content page? Gaetan ASP .Net 3 02-14-2006 07:24 PM



Advertisments