[Python-ideas] datetime.timedelta literals
Pål Grønås Drange
paal.drange at gmail.com
Mon Jun 4 06:59:22 EDT 2018
One thing that could solve both this proposal and the aforementioned
SI-proposition by Ken Kundert, could be supporting user-defined
literals. Suppose that __litXXX___ would make XXX a literal one could
use as a suffix for numbers and strings (and lists, dicts, sets?).
A user-defined literal could be defined as __lit<insert literal>__,
though I don't know how to import it.
Anything without leading underscore could be preserved for the standard
# In the standard library:
return complex(0, x)
# The above example would make 2+3j work as expected
# In the datetime module
# In the Pint (units) module for dimensional analysis
return x * pint.UnitRegistry().km
# It wouldn't be limited to numbers
s = 'literal'_up # s = LITERAL
# The _(x) from Django could be written
s = 'literal'_T # s = translate('literal')
# Heck, it could even be written as (abusing(?) notation)
s = 'literal'_ # s = translate('literal')
If we want to abuse the literals more, one could make
"""Makes a list into a sorted list"""
heap = _s # heap is of type sortedlist
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas