syntax philosophy
Dave Benjamin
ramen at lackingtalent.com
Tue Nov 18 16:49:26 EST 2003
More information about the Python-list mailing list
Tue Nov 18 16:49:26 EST 2003
- Previous message (by thread): syntax philosophy
- Next message (by thread): syntax philosophy
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
In article <hvjub.4572$sb4.1007 at newsread2.news.pas.earthlink.net>, Andrew Dalke wrote: > Similarly, dictionaries require that entries be created before they > can be used. This is because it's impossible for Python to > know which value you want for the default. Python is strongly > typed, so "2"+1 will raise an exception, unlike Perl where it > yields the number 3. If Python used a 0 for the default then > what if you really wanted to concatenate strings? If it used > "" then what if you wanted to add numbers? Whatever choice > you make, it will be wrong for most cases. 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. For instance--and I'm not saying this is better or worse, nor that it is in harmony with the Python Way--Ruby works like this instead: irb(main):001:0> d = {} => {} irb(main):002:0> d['a'] => nil irb(main):003:0> d.default = 0 => 0 irb(main):004:0> d['a'] => 0 irb(main):005:0> d.fetch('a') IndexError: key not found from (irb):5:in fetch' from (irb):5 So, as you can see, the default behavior in Ruby is akin to Python's dict.get (which allows you to supply a default), and the exception-throwing method is "fetch", instead of []. In summary: Python Ruby ------------- ---------------------- d['a'] d.fetch('a') d.get('a') d['a'] d.get('a', 0) d.default = 0; d['a'] I prefer the Python way because the lazier syntax ([]) is fail-fast. -- .:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:. : d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :
- Previous message (by thread): syntax philosophy
- Next message (by thread): syntax philosophy
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list