language design question
guthrie
guthrie at mum.edu
Sun Jul 9 23:58:26 EDT 2006
Steven Bethard wrote:
> Gregory Guthrie wrote:
>
>> For example,
>> - why is len() not a member function of strings? Instead one says
>> len(w).
>
> Why would ``x.len()`` be any more convenient than ``len(x)``? Your
> preference here seems pretty arbitrary.
-- Perhaps;
but having all standard operations as a method seems more regular (to
me), and allows a simple chained operation format of a series of method
calls, instead of alternating prefix function calls, and post-fix method
invocations; e.g.
x.lower().strip().toList().sort().join()
seems cleaner and simpler than the usage below, where the pre/post
alternation is visually more complex.
I think the mix of OO like methods, and global functions, is not ideal.
>
>> - Why doesn't sort() return a value?
>>
>> This would allow things like:
>> key = '',join( list(word.lower().strip()).sort() )
>
>
> Use sorted():
>
> key = ','.join(sorted(word.lower().strip()))
-- Thanks!
(Is the comma in ',' just a typo?)
>
>
>> - Another feature I assumed but it failed, is a nice default for
>> dictionaries, and more += like operations;
>> For example: to acculumate words in a dictionary -
>> dict[key] += [word]
>
>
> Get Python 2.5 and use collections.defaultdict:
-- Great, thanks.
>
> Python 2.5a2 (trunk:46491M, May 27 2006, 14:43:55) [MSC v.1310 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import collections
> >>> d = collections.defaultdict(int)
> >>> d['a'] += 5
> >>> d['b'] += 2
> >>> d
> defaultdict(<type 'int'>, {'a': 5, 'b': 2})
>
> STeVe
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
More information about the Python-list
mailing list