为什么python中没有post-pre增量运算符 [英] Why is there no post-pre increment operator in python

查看:119
本文介绍了为什么python中没有post-pre增量运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道为什么
python中没有post / pre增量运算符?

虽然声明:

++ j

有效但什么都不做

Anyone has any idea on why is there no post/pre increment operators in
python ?
Although the statement:
++j
works but does nothing

推荐答案

ri ************** @ gmail.com 写道:
任何人知道为什么在python中没有post / pre增量运算符?


简答:因为Guido不喜欢他们。


更长的答案:因为他们鼓励人们写出神秘的单行。

你真的没有什么不能和他们一起写的,如果没有他们你就不能写b $ b。它只需要另外一行或两行

代码。最终的结果可能会长一点,但是理解它几乎总是更容易



虽然声明:
++ j 工作但什么都不做
Anyone has any idea on why is there no post/pre increment operators in
python ?
Short answer: Because Guido didn''t like them.

Longer answer: Because they encourage people to write cryptic one-liners.
There really isn''t anything you can''t write with them that you couldn''t
write just as well without them. It just takes another line or two of
code. The end result may be a little longer, but it''s almost always easier
to understand.
Although the statement:
++j
works but does nothing




嗯,它的工作原理是它不是语法错误,但它不是
什么都不做。它将一元+运算符应用于j的值,然后

再次执行,然后抛弃结果。当然,这可能不是你所期望的,而且可能不是很有用,但它并不是很好,而是bb没什么。



Well, it works in the sense that it''s not a syntax error, but it doesn''t
quite do nothing. It applies the unary + operator to the value of j, then
does it again, then throws away the result. Granted, that''s probably not
what you expected, and probably not very useful, but it''s not quite
"nothing".


[ri ************** @ gmail.com]
[ri**************@gmail.com]
任何人都知道为什么没有帖子/ pre增加运算符在蟒蛇?


也许是因为Python不打算成为一个神秘的便携式组件

语言?这是我的猜测;-)

虽然声明:
++ j
有效但无效
Anyone has any idea on why is there no post/pre increment operators in
python ?
Maybe because Python doesn''t aim at being a cryptic portable assembly
language? That''s my guess ;-)
Although the statement:
++j
works but does nothing




这取决于j的类型,以及它如何实现__pos __()

方法。内置数值类型(整数,浮点数,复数)

实现__pos__来返回`self`的基类部分。那不是什么,而是什么都不做。没有++ Python中的运算符,BTW

- 这是一元加运算符的两个应用程序。



That depends on the type of j, and how it implements the __pos__()
method. The builtin numeric types (integers, floats, complex)
implement __pos__ to return the base-class part of `self`. That''s not
the same as doing nothing. There is no "++" operator in Python, BTW
-- that''s two applications of the unary-plus operator.

class MyFloat(float):
.... pass x = MyFloat(3.5)
x
3.5 type(x)
< class'' 。__main __ MyFloat '' > type(+ x)​​#" downcasts"基于`float`类型
< type''flora''> type(x .__ pos __())#same thing,but wordier
class MyFloat(float): .... pass x = MyFloat(3.5)
x 3.5 type(x) <class ''__main__.MyFloat''> type(+x) # "downcasts" to base `float` type <type ''float''> type(x.__pos__()) # same thing, but wordier



< type''flora''>


如果你愿意,你可以在你的班级实现__pos__,以便


+ a_riteshtijoriwala_object


向comp发布消息。 lang.c问为什么C太不灵活了;-)。


<type ''float''>

If you want, you can implement __pos__ in your class so that

+a_riteshtijoriwala_object

posts messages to comp.lang.c asking why C is so inflexible ;-).


ri ************** @ gmail.com 写道:
任何人都知道为什么没有帖子/在GUI中预增量运算符?


有很多好理由。

虽然声明:
++ j
有效但无效
Anyone has any idea on why is there no post/pre increment operators in
python ?
For lots of good reasons.
Although the statement:
++j
works but does nothing




所以--j。它们都用两个一元运算符解析为值:

连续应用于它:+(+(j))和 - ( - (j))。


< mike

-

Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/

独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。



So does --j. They both parse as a value with two unary operators
applied to it in succession: +(+(j)) and -(-(j)).

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


这篇关于为什么python中没有post-pre增量运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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