提案:Python中的十进制文字。 [英] Proposal: Decimal literals in Python.

查看:107
本文介绍了提案:Python中的十进制文字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python已经有了一段时间的Decimal数据类型。十进制数据

类型非常适合财务计算。使用这种数据类型对于计算机新手而言比浮动更直观,因为它的舍入行为

与人类期望的更接近。更重要的是:0.1和0.01

是精确的十进制而不是精确的浮点数。


不幸的是,访问Decimal数据类型并不是很容易。要获得

十进制数12.34,必须执行以下操作:


import decimal

x = decimal.Decimal(" ; 12.34")

当然我们可以将单个字符函数名称作为一个别名来引用,但是即使这样我们也必须使用两个括号

和每个十进制常量的引号。我们不能缩短

而不是D(12.34)


使用Python 3000无论如何都要对语言进行重大更改。我的

提案需要相对较小的更改。


我的建议:

- 任何以字母D为后缀的十进制常量;或d或d。将

解释为Decimal类型的文字。这也是

带指数表示法的十进制常量。


十进制文字的例子:

1d

12.34d

1e-3d(相当于0.001d)

1e3d(与1000d相同,但没有尾随零)。

1.25e5d(相同值为125000d,但没有尾随零)。


当我们打印十进制数字或将其转换为带有str()的字符串时,

不应添加尾随d。 repr函数产生的字符串带有

尾随d。


当从字符串转换十进制数字时,这两个数字都带有和
$ b $应该接受没有尾随d的b。


如果我们在核心语言中有十进制文字,我们应该在核心命名空间中有一个

类型的构造函数,例如dec()和int()

和float()。然后我们需要十进制模块只用于更高级的

之类的东西,比如setcontext。


优点:

- 十进制数据类型将更容易获得,特别是新手的


- 文字末尾的Traling字符已被使用(L

for很长)。

- 它与其他数字常量或

语言结构的语法不冲突。

- 它不会改变含义现有的有效文字常量。

过去常量的常量将继续这样做。


缺点:

- The Python解释器的词法扫描器会稍微复杂多点。

- 带有指数表示法的常量的d后缀很难看。

- 十进制使用exponentail表示法的数字如2e5d可能会被误认为是
for hex(由人类,而不是解析器,因为十六进制需要0x前缀)。

- 它需要十进制模块来成为一部分核心Python

口译员。


-

Lennart

解决方案

Lennart Benschop< le ****** @ xs4all.nlwrites:


Python有十进制数据现在输入一段时间了。



从版本2.4< URL:http://www.python.org/dev/peps/pep-0327>。


不幸的是,访问十进制数据

类型不是很容易。要获得十进制数12.34,必须做一些事情

像这样:


导入十进制

x = decimal.Decimal(" ; 12.34")



稍微好一点,如果你需要创建很多::


>>来自十进制导入十进制
foo =十进制(" 12.34")


当然我们可以将单个字符函数名称作为Decimal类型构造函数的

别名,但即便如此我们也必须使用

括号和每个十进制常量的引号。我们

不能使它短于D(12.34)



我们也不应该。函数或类型名称应该简短但明确,

和''Decimal''大约和我想要的一样短。


使用Python 3000



" Python 3000"现在处于测试阶段,被称为Python版本

3.0。


无论如何都要对语言进行重大更改。



所有这些都已经确定了一段时间了。引入行为变化并希望他们使用Python 3.0已经太晚了



我的建议:



随意使用PEP流程提交实际提案。您可能需要

想要参考引入了Decimal类型的PEP来获得灵感。


-

\这里有多少人有远程传授能力?举起我的手。 |

` \ - Emo Philips |

_o__)|

Ben Finney


On Fri,2007年10月26日19:19:46 +1000,Ben Finney写道:


>当然我们可以为Decimal类型构造函数引入单个字符函数名称作为别名,但即使这样,我们也必须对每个十进制常量使用
括号和引号。我们不能把它缩短到D(12.34)



我们也不应该。函数或类型名称应该简短但明确,并且

''十进制''大约和我想要的一样短。



你不喜欢str,int,float,list,set,dict或bool?


或者就此而言,enum
http:// www。 python.org/dev/peps/pep-0354/

*眨眼*

-

史蒂文。


Steven D''Aprano< st *** @ REMOVE-THIS-cybersource.com.auwrites:


On Fri,2007年10月26日19:19:46 +1000,Ben Finney写道:


我们也不应该。一个函数或类型名称应该很短,但是明确的是&b
,而''Decimal''就像我想要的那么短。



你不喜欢str,int,float,list,set,dict或bool?



他们都会为小数类型制作糟糕的名字。


-

\无论你做什么都是微不足道的,但它非常重要。

` \非常重要的是你做到了。 - 圣雄甘地|

_o__)|

Ben Finney


Python has had the Decimal data type for some time now. The Decimal data
type is ideal for financial calculations. Using this data type would be
more intuitive to computer novices than float as its rounding behaviour
matches more closely what humans expect. More to the point: 0.1 and 0.01
are exact in Decimal and not exact in float.

Unfortunately it is not very easy to access the Decimal data type. To obtain
the decimal number 12.34 one has to do something like this:

import decimal
x=decimal.Decimal("12.34")

Of course we can intruduce a single character function name as an alias for
the Decimal type constructor, but even then we have to use both parentheses
and quotes for each and every decimal constant. We cannot make it shorter
than D("12.34")

With Python 3000 major changes to the language are carried out anyway. My
proposal would require relatively minor changes.

My proposal:
- Any decimal constant suffixed with the letter "D" or "d" will be
interpreted as a literal of the Decimal type. This also goes for
decimal constants with exponential notation.

Examples of decimal literals:
1d
12.34d
1e-3d (equivalent to 0.001d)
1e3d (same value as 1000d, but without trailing zeros).
1.25e5d (same value as 125000d but without trailing zeros).

When we print a decimal number or convert it to a string with str(), a
trailing d should not be added. The repr function does yield strings with a
trailing d.

When decimal numbers are converted from strings, both numbers with and
without a trailing d should be accepted.

If we have decimal literals in the core language, we should probably have a
type constructor in the core namespace, e.g. dec() along with int()
and float(). We then need the decimal module only for the more advanced
stuff like setcontext.

Pros:
- the Decimal data type will be more readily accessible, especially
for novices.
- Traling characters at the end of a literal are already used (the L
for long).
- It does not conflict with the syntax of other numeric constants or
language constructs.
- It does not change the meaning of existing valid literal constants.
Constants that used to be float will continue to do so.

Cons:
- The lexical scanner of the Python interpreter will be slightly more
complex.
- The d suffix for constants with exponential notation is ugly.
- Decimal numbers with exponentail notation like 2e5d could be mistaken
for hex (by humans, not by the parser as hex requires the 0x prefix).
- It requires the decimal module to be part of the core Python
interpreter.

--
Lennart

解决方案

Lennart Benschop <le******@xs4all.nlwrites:

Python has had the Decimal data type for some time now.

Since version 2.4 <URL:http://www.python.org/dev/peps/pep-0327>.

Unfortunately it is not very easy to access the Decimal data
type. To obtain the decimal number 12.34 one has to do something
like this:

import decimal
x=decimal.Decimal("12.34")

Slightly nicer, if you need to create a lot of them::

>>from decimal import Decimal
foo = Decimal("12.34")

Of course we can intruduce a single character function name as an
alias for the Decimal type constructor, but even then we have to use
both parentheses and quotes for each and every decimal constant. We
cannot make it shorter than D("12.34")

Nor should we. A function or type name should be short but explicit,
and ''Decimal'' is about as short as I''d want.

With Python 3000

"Python 3000" is now in beta development, and is called Python version
3.0.

major changes to the language are carried out anyway.

All of which have been decided for some time now. It''s rather too late
to introduce behaviour changes and hope for them to be in Python 3.0.

My proposal:

Feel free to file an actual proposal using the PEP process. You might
want to refer to the PEP that introduced the Decimal type for
inspiration.

--
\ "How many people here have telekenetic powers? Raise my hand." |
`\ -- Emo Philips |
_o__) |
Ben Finney


On Fri, 26 Oct 2007 19:19:46 +1000, Ben Finney wrote:

>Of course we can intruduce a single character function name as an alias
for the Decimal type constructor, but even then we have to use both
parentheses and quotes for each and every decimal constant. We cannot
make it shorter than D("12.34")


Nor should we. A function or type name should be short but explicit, and
''Decimal'' is about as short as I''d want.

You don''t like str, int, float, list, set, dict, or bool?

Or for that matter, enum
http://www.python.org/dev/peps/pep-0354/

*wink*
--
Steven.


Steven D''Aprano <st***@REMOVE-THIS-cybersource.com.auwrites:

On Fri, 26 Oct 2007 19:19:46 +1000, Ben Finney wrote:

Nor should we. A function or type name should be short but
explicit, and ''Decimal'' is about as short as I''d want.


You don''t like str, int, float, list, set, dict, or bool?

They''d all make lousy names for a decimal type.

--
\ "Whatever you do will be insignificant, but it is very |
`\ important that you do it." -- Mahatma Gandhi |
_o__) |
Ben Finney


这篇关于提案:Python中的十进制文字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆