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
> 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
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.