syntax philosophy
Andrew Dalke
adalke at mindspring.com
Tue Nov 18 18:13:54 EST 2003
Dave Benjamin
> I agree with everything else you said here, but want to caution against
your
> claim that it's "impossible for Python to know" what to supply as a
default.
The full quote is 'This is because it's impossible for Python to know which
value you want for the default'. Note that "what you want".
I later said:
Whatever choice you make [should have said "Python makes"], it
will be wrong for most cases.
> irb(main):002:0> d['a']
> => nil
In most cases, this is the wrong return value, yes?
> irb(main):003:0> d.default = 0
> => 0
And there's where you tell Ruby what you want.
> d.get('a', 0) d.default = 0; d['a']
>
> I prefer the Python way because the lazier syntax ([]) is fail-fast.
And because it's thread-safe.
I do see your point that 'get' does provide a default value of
None if the item doesn't exist, but the behaviour, given that
the default is passed as part of the function parameter, means
that that value isn't really associated with the dictionary.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list