关于 Python 的混合数字数据类型将结果转换为最复杂的操作数
一点背景知识:我正在通过 O'Reilly 的《学习 Python》一书学习 Python,我在 Java 方面有了一些经验。
不管怎样,在阅读第 5 章时(实际上我还在读),我遇到了一个关于 Python 处理混合数字表达式结果的方式的问题。 在书中,他们使用了混合整数和浮点数 (40 + 3.14) 的示例,并继续解释该表达式的结果将是浮点数,因为 Python 将操作数向上转换 em> 为最复杂操作数的类型。
我的问题是:程序员不必记住哪个数字操作数最高,并记住结果将“升级”为该格式,为结果类型创建一个特殊的数字文字不是更简单吗?
我的逻辑是这样的:如果你的表达式中有小数位,你就知道它将是一个浮点数,如果你有类似 3+4j 的东西,你就知道它将是一个复数。 为什么您必须记住数字文字的层次结构才能知道您的结果将被视为什么? 在我看来,将结果分配给单个、不知情的文字似乎是一个更简单的过程,因为知道无论表达式是否具有混合数字,它都会被视为特定的数据类型。
后续问题:目前是否有一种语言正在执行这种事情?
再说一次,我对Python的了解有限,所以这可能是一个愚蠢的问题,但我想知道为什么程序员必须让自己经历这个过程。 我可以想象目前还没有某种系统的唯一原因是,结果的特定数字类型可能不像其他语言(Java)中那么重要。
A little background: I'm in the process of learning Python through O'Reilly's, "Learning Python" book, I've had some experience in Java.
Anyway, upon reading Chapter 5 (I'm still in the middle of it, actually) I have come across a question with the way Python treats results of Mixed Numeric expressions. In the book, they use an example of mixing an integer and a floating-point number (40 + 3.14) and proceed to explain that the result of this expression would be a floating-point number because Python converts operands up to the type of the most complicated operand.
My question is this: Instead of programmers having to remember which Numeric operand is the highest and remember that the that the result will be "upped" to that format, wouldn't it be simpler to create a special Numeric Literal for result types?
My logic is this: If you have a decimal place in your expression, you know it's going to be a floating point number, if you have something like 3+4j, you know it's going to be a complex number. Why should you have to remember the hierarchy of Numeric Literals just to know what your result is going to be treated as? In my opinion, it seems like it would be a much simpler process to assign results to a single, uninformed Literal to know that regardless of whether or not the expression has Mixed Numerics, it will be treated as a specific Data Type.
Follow up question: Is there a language where this kind of thing is currently being preformed?
Again, my knowledge of Python is limited, so this may be a silly question, but I would like to know why programmers have to put themselves through this process. The only reason why I could imagine that there isn't a system of some kind in place already is that perhaps the specific Numeric Type of a result isn't as important as it is in some other languages (Java).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是“数字文字的层次结构”。 我不太确定你还想要什么。 此外,结果将始终是numbers.Number的子类,因此您实际上确实对结果对象的类型有一定的保证。
That is the "hierarchy of Numeric Literals". I'm not really sure what more you want. Furthermore, the result will always be a subclass of numbers.Number, so you actually do have some guarantee about what type the resulting object will be.
假设您有一个统一的数字类型,并且您输入了以下语句:
这与您今天得到的有什么不同?
这已经是有效的 Python 了。 唯一的区别是
type(a)
、type(b)
、type(c)
返回int
,float
和complex
,我猜您希望它们全部返回类似number
的内容。 但除非你愿意/必须,否则你永远不会真正处理这个问题。拥有类似方案的数字塔是有原因的。 如果您知道自己只处理整数,则可以利用硬件进行整数计算。 或者,当您知道要将某种用户输入限制为整数时,您可以从
int
类型派生。我确信您可以找到一种具有某种统一编号的类型系统的语言。 但我不确定我是否理解了你的论点。 也许我在你的问题中遗漏了一些东西?
Suppose you had a unified numeric type and you typed the following statements:
How would this be any different from what you get today?
This is already valid Python. The only difference is that
type(a)
,type(b)
,type(c)
returnint
,float
andcomplex
, and I guess you want them all to return something likenumber
. But you never really deal with that unless you want/have to.There are reasons for having something like a scheme-like numerical tower. You may take advantage of the hardware for integer calculations if you know you're only dealing with itnegers. Or you may derive from the
int
type when you know you want to restrict some kind of user input to an integer.I'm sure you can find a language with a type system that has some kind of unified number. But I'm not sure I've understood your argument. Perhaps I've missed something in your question?
我不认为我真的理解问题:
你的意思是应该明确指定操作结果?
你可以通过显式强制转换来做到这一点
浮动(1+0.5)
整数(1+0.5)
complex(1+0.5)
您的意思是运算符应该只接受相同类型的操作数吗?
1+2
1+0.5-> 引发异常
1+整数(0.5)
浮动(1)+0.5
虽然有道理,但会引入太多冗长的内容,并且 int->float 转换总是成功的,并且不会导致精度损失(除了非常大的数字)
不同返回类型的单独操作数:
1 + 0.5 -> 整数
1 `float_plus` 2 -> 浮动
重复显式强制转换功能,并且完全病态
Don't think i really understood question:
Do you mean that operation result should be explictly specified?
you can do it with explict cast
float(1+0.5)
int(1+0.5)
complex(1+0.5)
Do you mean that operators should accept only same type operands?
1+2
1+0.5 -> raises exception
1+int(0.5)
float(1)+0.5
while having sense, it would introduce too much verbosity and int->float casts are always successful and don't lead to precision loss (except really large numbers)
Separate operands for different return types:
1 + 0.5 -> int
1 `float_plus` 2 -> float
Duplicates explict cast functionality and is plain sick