Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Calling functions: Why this complicated ?

0 views
Skip to first unread message

Chris Rebert

unread,
Jul 14, 2009, 7:31:25 PM7/14/09
to Mohan Parthasarathy, python-list
On Tue, Jul 14, 2009 at 1:40 PM, Mohan Parthasarathy<suru...@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

Tim Rowe

unread,
Jul 15, 2009, 6:54:06 AM7/15/09
to python-list
2009/7/15 Jeremiah Dodds <jeremia...@gmail.com>:

> As a hopefully semi-informative aside, I've been writing python code for a
> few years now, and I regularly use all four forms of argument passing listed
> above.

Curiously, I never use the all-named style in Python, whereas it's my
normal style in Ada. I shall now enter a period of self-refelection to
try to work out why I am so inconsistent :-)


--
Tim Rowe

lkcl

unread,
Aug 1, 2009, 7:19:55 PM8/1/09
to
On Jul 14, 11:31 pm, Chris Rebert <c...@rebertia.com> wrote:

> On Tue, Jul 14, 2009 at 1:40 PM, Mohan Parthasarathy<surut...@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 toJavaScript. 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*)

yehh, that's what i thought, originally, but after talking it over
with the people doing the EMCAScript Harmony spec, they explained that
nooo, there _are_ exceptions thrown (two if the browser implements it
properly, which IE of course doesn't). you can try it by installing
the spidermonkey js compiler, which is the exact same js engine that's
in firefox.

in the python-to-javascript compiler, http://pyjs.org, however, we've
taken to adding a double-check (the equivalent of getattr) when you
specify the --strict option, juuust to make sure.

and yes, there was a talk at europython 2009, pretty much called
"javascript, the good parts, for python programmers", you can look it
up, the talk audio is online by now. http://europython.eu

l.

0 new messages