Chr*_*utz 990
++不是运营商.这是两个+运营商.该+运营商的身份运营,这什么都不做.(澄清:+和-一元运算符只能处理数字,但我认为你不会指望一个假设的++运算符来处理字符串.)
++count
Run Code Online (Sandbox Code Playgroud)
解析为
+(+count)
Run Code Online (Sandbox Code Playgroud)
这转化为
count
Run Code Online (Sandbox Code Playgroud)
您必须使用稍长的+=运算符来执行您想要执行的操作:
count += 1
Run Code Online (Sandbox Code Playgroud)
我怀疑++和--操作员是因为一致性和简单性而被排除在外.我不知道Guido van Rossum给出的确切论据,但我可以想象一些论点:
++count是模糊的,因为它可能是+,+,count(两个一元+经营者)一样容易,因为它可能是++,count(一个一元++运算符).这不是一个重要的句法歧义,但确实存在.++只不过是一个同义词+= 1.这是一个简化发明,因为C编译器是愚蠢的,不知道如何优化大多数计算机a += 1的inc指令.在优化编译器和字节码解释语言的今天,向一种语言添加运算符以允许程序员优化其代码通常是不受欢迎的,特别是在像Python这样设计为一致且可读的语言中.++运算符的语言中的一个常见新手错误是混合了增量前/减值运算符之间的差异(优先级和返回值),Python喜欢消除语言"gotcha"-s.该优先问题的前/后加用C是相当毛,和令人难以置信的容易陷入困境.Len*_*bro 378
当您想要递增或递减时,通常希望对整数执行此操作.像这样:
b++
Run Code Online (Sandbox Code Playgroud)
但在Python中,整数是不可变的.那是你无法改变它们.这是因为整数对象可以在多个名称下使用.试试这个:
>>> b = 5
>>> a = 5
>>> id(a)
162334512
>>> id(b)
162334512
>>> a is b
True
Run Code Online (Sandbox Code Playgroud)
上面的a和b实际上是同一个对象.如果你增加a,你也会增加b.那不是你想要的.所以你必须重新分配.像这样:
b = b + 1
Run Code Online (Sandbox Code Playgroud)
或者更简单:
b += 1
Run Code Online (Sandbox Code Playgroud)
哪个会重新分配b给b+1.这不是增量运算符,因为它不会递增b,而是重新分配它.
简而言之:Python在这里表现不同,因为它不是C,并且不是机器代码的低级包装器,而是高级动态语言,其中增量没有意义,也不像C中那样必要例如,每次有循环时都使用它们.
glg*_*lgl 50
虽然其他答案是正确的,只要它们表明+通常只是做什么(即,保留数字,如果它是一个),它们是不完整的,因为它们不解释会发生什么.
确切的说,+x计算结果为x.__pos__()和++x到x.__pos__().__pos__().
我可以想象一个非常奇怪的类结构(儿童,不要在家里这样做!)像这样:
class ValueKeeper(object):
def __init__(self, value): self.value = value
def __str__(self): return str(self.value)
class A(ValueKeeper):
def __pos__(self):
print 'called A.__pos__'
return B(self.value - 3)
class B(ValueKeeper):
def __pos__(self):
print 'called B.__pos__'
return A(self.value + 19)
x = A(430)
print x, type(x)
print +x, type(+x)
print ++x, type(++x)
print +++x, type(+++x)
Run Code Online (Sandbox Code Playgroud)
RBF*_*F06 19
Python 没有一元递增/递减运算符 ( --/ ++)。相反,要增加一个值,请使用
a += 1
Run Code Online (Sandbox Code Playgroud)
但是这里要小心。如果你来自 C,即使这在 python 中也是不同的。Python 没有像 C 那样的“变量”,而是 python 使用名称和对象,在 python 中int是不可变的。
所以让我们说你做
a = 1
Run Code Online (Sandbox Code Playgroud)
这在 python 中意味着:创建一个int具有值的类型的对象1并将名称绑定a到它。的对象是的一个实例int具有值1,并且名称 a是指它。名称a和它所指的对象是不同的。
现在让我们说你做
a += 1
Run Code Online (Sandbox Code Playgroud)
由于ints 是不可变的,这里发生的事情如下:
a引用的对象(它是一个intwith id 0x559239eeb380)0x559239eeb380(它是1)int对象2(它具有对象 id 0x559239eeb3a0)a到这个新对象a指的是 object0x559239eeb3a0并且原来的 object( 0x559239eeb380) 不再被名称所指了a。如果没有任何其他名称引用原始对象,则稍后将对其进行垃圾收集。自己试试看:
a = 1
print(hex(id(a)))
a += 1
print(hex(id(a)))
Run Code Online (Sandbox Code Playgroud)
Pio*_*ski 12
Python没有这些运算符,但如果你真的需要它们,你可以编写一个具有相同功能的函数.
def PreIncrement(name, local={}):
#Equivalent to ++name
if name in local:
local[name]+=1
return local[name]
globals()[name]+=1
return globals()[name]
def PostIncrement(name, local={}):
#Equivalent to name++
if name in local:
local[name]+=1
return local[name]-1
globals()[name]+=1
return globals()[name]-1
Run Code Online (Sandbox Code Playgroud)
用法:
x = 1
y = PreIncrement('x') #y and x are both 2
a = 1
b = PostIncrement('a') #b is 1 and a is 2
Run Code Online (Sandbox Code Playgroud)
在函数内部,如果要更改局部变量,则必须添加locals()作为第二个参数,否则它将尝试更改全局变量.
x = 1
def test():
x = 10
y = PreIncrement('x') #y will be 2, local x will be still 10 and global x will be changed to 2
z = PreIncrement('x', locals()) #z will be 11, local x will be 11 and global x will be unaltered
test()
Run Code Online (Sandbox Code Playgroud)
您还可以使用以下功能:
x = 1
print(PreIncrement('x')) #print(x+=1) is illegal!
Run Code Online (Sandbox Code Playgroud)
但在我看来,以下方法更加清晰:
x = 1
x+=1
print(x)
Run Code Online (Sandbox Code Playgroud)
减少运营商:
def PreDecrement(name, local={}):
#Equivalent to --name
if name in local:
local[name]-=1
return local[name]
globals()[name]-=1
return globals()[name]
def PostDecrement(name, local={}):
#Equivalent to name--
if name in local:
local[name]-=1
return local[name]+1
globals()[name]-=1
return globals()[name]+1
Run Code Online (Sandbox Code Playgroud)
我在我的模块中使用这些函数将javascript转换为python.
Vit*_*nko 11
在Python中,与Common Lisp,Scheme或Ruby等语言相比,严格执行表达式和语句之间的区别.
因此,通过引入这样的运算符,您将打破表达式/语句拆分.
出于同样的原因你不能写
if x = 0:
y = 1
Run Code Online (Sandbox Code Playgroud)
正如你可以在其他语言中那样,不保留这种区别.
是的,我也错过了++和-功能。几百万行c代码使这种思想深深地扎根在我的脑海中,而不是与之抗争……这是我拼凑而成的一类,实现了:
pre- and post-increment, pre- and post-decrement, addition,
subtraction, multiplication, division, results assignable
as integer, printable, settable.
Run Code Online (Sandbox Code Playgroud)
这是:
class counter(object):
def __init__(self,v=0):
self.set(v)
def preinc(self):
self.v += 1
return self.v
def predec(self):
self.v -= 1
return self.v
def postinc(self):
self.v += 1
return self.v - 1
def postdec(self):
self.v -= 1
return self.v + 1
def __add__(self,addend):
return self.v + addend
def __sub__(self,subtrahend):
return self.v - subtrahend
def __mul__(self,multiplier):
return self.v * multiplier
def __div__(self,divisor):
return self.v / divisor
def __getitem__(self):
return self.v
def __str__(self):
return str(self.v)
def set(self,v):
if type(v) != int:
v = 0
self.v = v
Run Code Online (Sandbox Code Playgroud)
您可以这样使用它:
c = counter() # defaults to zero
for listItem in myList: # imaginary task
doSomething(c.postinc(),listItem) # passes c, but becomes c+1
Run Code Online (Sandbox Code Playgroud)
...已经有了c,您可以执行此操作...
c.set(11)
while c.predec() > 0:
print c
Run Code Online (Sandbox Code Playgroud)
....要不就...
d = counter(11)
while d.predec() > 0:
print d
Run Code Online (Sandbox Code Playgroud)
...并用于(重新)分配为整数...
c = counter(100)
d = c + 223 # assignment as integer
c = c + 223 # re-assignment as integer
print type(c),c # <type 'int'> 323
Run Code Online (Sandbox Code Playgroud)
...这将使c保持为类型计数器:
c = counter(100)
c.set(c + 223)
print type(c),c # <class '__main__.counter'> 323
Run Code Online (Sandbox Code Playgroud)
编辑:
然后还有一些意想不到的(并且完全是不想要的)行为,
c = counter(42)
s = '%s: %d' % ('Expecting 42',c) # but getting non-numeric exception
print s
Run Code Online (Sandbox Code Playgroud)
...因为在该元组中,没有使用getitem(),而是将对对象的引用传递给格式函数。叹。所以:
c = counter(42)
s = '%s: %d' % ('Expecting 42',c.v) # and getting 42.
print s
Run Code Online (Sandbox Code Playgroud)
…或更确切地说,是我们实际上想要发生的事情,尽管冗长程度以实际形式c.v相反(使用代替)……
c = counter(42)
s = '%s: %d' % ('Expecting 42',c.__getitem__()) # and getting 42.
print s
Run Code Online (Sandbox Code Playgroud)
在 python 3.8+ 中,您可以执行以下操作:
(a:=a+1) #same as ++a (increment, then return new value)
(a:=a+1)-1 #same as a++ (return the incremented value -1) (useless)
Run Code Online (Sandbox Code Playgroud)
你可以用这个做很多思考。
>>> a = 0
>>> while (a:=a+1) < 5:
print(a)
1
2
3
4
Run Code Online (Sandbox Code Playgroud)
或者,如果你想用更复杂的语法编写一些东西(目标不是优化):
>>> del a
>>> while (a := (a if 'a' in locals() else 0) + 1) < 5:
print(a)
1
2
3
4
Run Code Online (Sandbox Code Playgroud)
即使'a'不存在没有错误,它也会返回0,然后将其设置为1
| 归档时间: |
|
| 查看次数: |
926240 次 |
| 最近记录: |