Calling functions: Why this complicated ?

Chris Rebert clp2 at rebertia.com
Tue Jul 14 19:31:25 EDT 2009


On Tue, Jul 14, 2009 at 1:40 PM, Mohan Parthasarathy<suruti94 at gmail.com> wrote:
> Hi,
> I am a newbie. I am reading
> http://www.network-theory.co.uk/docs/pytut/KeywordArguments.html
> Defining a function with "N" arguments and calling them in "M" different
> ways. Why does it have to be this complicated ? I like the idea of calling
> the function by explicitly naming the arguments, but there are so many other
> ways here that is very confusing. Do people really use all these features ?
> Perhaps, there is a equivalent book to "Javascript: Good Parts" for Python ?

Oh $DEITY, don't even compare Python to JavaScript. At least in
Python, when you try to access a non-existent attribute, a proper
NameError exception is thrown rather than silently getting back
"undefined"... (*has traumatic horror story flashback*)

The calling syntax is not so much complicated as it is liberating.
Are you a C junkie who has never heard of named arguments?
    Just use the call sequence like you've always done.
Are you a exacting Smalltalk or Objective-C person who likes to name
all the arguments all the time for clarity?
    You can do that.
Do you want to call a function with lots of default arguments but only
want to override a couple of them?
    Specifying them by name lets you do that succinctly.
Do you not want to have to worry about the order of the parameters
because it seems kinda arbitrary?
    Then specify the arguments by name.
etc...

And if you try and specify an argument twice, Python will throw an
error, so anything truly confusing will get caught right away.
And there's only one definition syntax, so while the call could be
complicated, figuring out what it means by looking to the definition
is fairly easy.

There really aren't that many ways it's done in practice. In practice,
the following styles cover 90% of cases:
- All named arguments: foo(bar=a, baz=b, qux=c)
- All sequential arguments: foo(a, b, c)
- All sequential arguments, with a few optional arguments given by
name: foo(a, b, c, flag=True, style=qux)
- Simple pass-through: foo(*args, **kwargs)

Granted, there's 4 of them, but each taken by itself seems pretty easy
to read, IMHO.

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list