在我必须维护的项目中有以下一些Python代码:
# If the `factor` decimal is given, compute new price and a delta
factor = +factor.quantize(TWOPLACES)
new_price = +Decimal(old_price * factor).quantize(TWOPLACES)
delta = new_price - old_price
Run Code Online (Sandbox Code Playgroud)
这里的问题是+变量前面的目的.
Python文档调用它一元加运算,其"产生的数值参数不变".它可以安全地移除吗?
(顺便提一下,代码是我前一段时间编写的,希望我已经吸取了教训 - 如果测试存在,或者如果在评论中澄清使用小数一元加上,那么这不是一个问题.)
我需要一些帮助来计算Pi.我正在尝试编写一个将Pi计算为X位数的python程序.我已经尝试过python邮件列表中的几个,这对我的使用来说很慢.我已经阅读了有关Gauss-Legendre算法的内容,我尝试将其移植到Python但没有成功.
我正在读这里,我很感激我输入错误的地方!
输出:0.163991276262
from __future__ import division
import math
def square(x):return x*x
a = 1
b = 1/math.sqrt(2)
t = 1/4
x = 1
for i in range(1000):
y = a
a = (a+b)/2
b = math.sqrt(b*y)
t = t - x * square((y-a))
x = 2* x
pi = (square((a+b)))/4*t
print pi
raw_input()
Run Code Online (Sandbox Code Playgroud) 我不小心写道:
total_acc =+ accuracy
Run Code Online (Sandbox Code Playgroud)
代替:
total_acc += accuracy
Run Code Online (Sandbox Code Playgroud)
我在网上搜索,找不到任何东西.那么发生了什么,为什么Python认为我的意思是我在打字?
计算机太信任我们了.:)
(版本 1.13+)中有一个positive函数numpy,它似乎什么都不做:
In [1]: import numpy as np
In [2]: A = np.array([0, 1, -1, 1j, -1j, 1+1j, 1-1j, -1+1j, -1-1j, np.inf, -np.inf])
In [3]: A == np.positive(A)
Out[3]:
array([ True, True, True, True, True, True, True, True, True,
True, True])
Run Code Online (Sandbox Code Playgroud)
文档说: Returned array or scalar: `y = +x`
这个函数的用例是什么?
所以我有一段简单的代码打印出整数1-10:
i = 0
while i < 10:
i += 1
print(i)
Run Code Online (Sandbox Code Playgroud)
然后,如果您只是在第3行更改一个运算符,它会打印出无限量的1个整数(我明白为什么会这样做).为什么运行第二个程序时不会出现语法错误?如果赋值运算符后跟一个加法运算符,它会不会调用语法错误?
i = 0
while i < 10:
i =+ 1
print(i)
Run Code Online (Sandbox Code Playgroud) 我正在写一个类似的函数计算eval。回想起来float.__pos__,我真的不知道为什么会有这么一个无用的功能。因为:
>>> float.__pos__(-1.0)
-1.0
>>> float.__pos__(1.0)
1.0
Run Code Online (Sandbox Code Playgroud)
whilefloat.__neg__完全不同:
>>> float.__neg__(1.0)
-1.0
Run Code Online (Sandbox Code Playgroud)
那么 的意义是什么float.__pos__?