[Python-ideas] non-Pre-PEP: Syntactic replacement of built-in types with user-defined callables (Take 2... fewer newlines!)
Talin
talin at acm.org
Tue Jan 29 04:44:23 CET 2008
Taro wrote:
> Hi all... again,
>
> Built-in types such as float, string, or list are first-class citizens
> in Python sourcefiles, having syntactic support:
> myfloat = 1.0
> mystring = "my string"
> mylist = [1,2,4,8]
> mydict = {1:"a", 2:"b", 3:"c"}
> myset = {1,2,3}
>
> User-defined classes are second-class citizens, requiring data to be
> manually converted from a type:
> mydecimal = Decimal("1.00000000000000001")
> myrope = Rope("my rope")
> myblist = BList([1,2,4,8])
> myordereddict = OrderedDict((1,"a"), (2, "b"), (3, "c"))
> myfrozenset = frozenset([1,2,3])
The problem I see with this is, what if you need to use both decimals
and floats together?
I've often thought that there should be a shorter spelling for decimal
numbers, but I was thinking that a simple suffix letter would suffice:
mydecimal = 1.0000000000000001d
(This assumes of course that the compiler knows how to form a decimal
constant, although the actual construction of the constant could be
deferred until runtime.)
And if you really are suffering repetitive strain injury from having to
type 'OrderedDict' 100 times in your code, it seems to me that you could
just avoid creating each one individually, and instead have an array of
inputs which gets converted to an array of OrderedDict. In other words,
one generally doesn't see code where the same repeated item is assigned
to 100 individual variables; Most programmers, when they see more than
half a dozen similar items, will start thinking about ways in which they
can roll up the definitions into an algorithm to generate them.
-- Talin
More information about the Python-ideas
mailing list