我编写了一个小的 Python 脚本来创建文件和计算时间。我已经在 Fedora 10 和 Ubuntu 8.x 上测试过它并且运行良好。 python 版本是 2.5.x。
我试着在我的生产服务器(一个老式的基于 red hat 的 linux 服务器)上运行它,python 的版本是 2.2.3。该脚本不起作用,并在类定义中引发语法错误。
该脚本定义了一个类,该类具有在磁盘上创建文件、测量时间、估计磁盘写入速度的方法。它是这样开始的:
class TestDiskSpeed():
def __init__(self, rounds=1, speedMode=SPEED_MODE_MEGABYTE):
类定义被 python 2.2.3 指出为错误。
自 2.2.3 以来 python 的主要变化有哪些可能导致我的应用程序崩溃?我正在使用这些模块:os、sys、time、stat、gc。
更新:
通过从类定义中删除 ()
python 接受了该类。但它在这一行引发了另一个错误:
size = long(size) if size != None else self._size
我是一名 PHP 开发人员,刚接触 python 编程(可能一个月),并且非常习惯在 PHP 中完成的三元运算,如下所示:
$var = (condition) ? $valueIfTrue : $valueIfFalse;
我搜索并发现它是在 python 中完成的,就像我用于我的 size
变量的那样。尽管 python 2.2.3 似乎不喜欢它。
我打算在这里插入所有代码(无论如何我都会将脚本发布为 LGPL),但代码超过 150 行(仍在开发中)。
最佳答案
我刚才写了一个脚本来帮助回答这个确切的问题:pyqver .
This script attempts to identify the minimum version of Python that is required to execute a particular source file.
When developing Python scripts for distribution, it is desirable to identify which minimum version of the Python interpreter is required. pyqver attempts to answer this question using a simplistic analysis of the output of the Python compiler.
使用 -v
选项列出您的脚本需要特定最低版本的原因。这对于决定更改哪些内容以使您的脚本适用于特定版本的 Python 非常有帮助。
一如既往,随时 fork 和贡献!
关于python - 自版本 2.2.3 以来 python 的主要变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1824417/