我正在阅读有关静态变量(即类变量),静态方法,类方法以及所有这些变量的细微差别。在像C ++这样的语言中,static variables are generally strongly discouraged because of many IMO compelling reasons。我只是好奇,如果Python社区对静态变量的已知位置不鼓励。 The Google Python Style Guide does not seem to have a pro/con position on static variables
在Python语言中,不鼓励静态变量(即代码异味)?
谷歌C ++风格指南的声明“[静态变量]导致难以发现的错误”当然不仅适用于C ++语言,也适用于Python语言。
答案 0 :(得分:2)
在某些语言(如Java)中,静态变量可能会导致可测试性问题,因为它们无法轻易模拟出来。这在Python中并非如此。
但是,静态和类变量的好处较少,因为当您拥有不需要访问特定实例成员的值或函数时,您可以在模块级别定义它们,并在不使用它们的情况下使用它们引用一个类 - 一种像Java这样的语言不具备的奢侈品。
所以我会说static
本身并不是代码气味。但如果你的一半变量是static
,那么我会问一些关于你对OO设计理解的问题。
答案 1 :(得分:1)
您正在阅读的C ++资源正在讨论类型为static storage duration的变量。那些与静态变量不同;例如,以下变量也具有静态存储持续时间:
$scope.count = 2;
$scope.tableHeaders = [["TabNo", "Document", "PageNumber", "Date"],["TabNo", "Document", "PageNumber"]];
$scope.documentsList = [];
$scope.documentsList.push(JSON.parse('[{"idANX":1,"pathANX":"Product Overview.pdf","isHeaderANX":1,"isExcludedANX":0,"isNotHyperlinkedANX":0,"isSubHeaderANX":0,"HyperlinkedColumnANX":1,"TabNo":{"colspan":0,"rowspan":0,"hyperlinkedDoc":"","isLastCell":false,"value":"1"},"Document":{"colspan":0,"rowspan":0,"hyperlinkedDoc":"Product Overview.pdf","isLastCell":false,"value":"Product Overview"},"PageNumber":{"colspan":0,"rowspan":0,"hyperlinkedDoc":"","isLastCell":false,"value":"1 - 2"},"Date":{"colspan":0,"rowspan":0,"hyperlinkedDoc":"","isLastCell":true,"value":"18 August 2015"}},{"idANX":2,"pathANX":"Spec.pdf","isHeaderANX":0,"isExcludedANX":0,"isNotHyperlinkedANX":0,"isSubHeaderANX":0,"HyperlinkedColumnANX":1,"TabNo":{"colspan":0,"rowspan":0,"hyperlinkedDoc":"","isLastCell":false,"value":"2"},"Document":{"colspan":0,"rowspan":0,"hyperlinkedDoc":"Spec.pdf","isLastCell":false,"value":"Spec"},"PageNumber":{"colspan":0,"rowspan":0,"hyperlinkedDoc":"","isLastCell":false,"value":"3 - 4"},"Date":{"colspan":0,"rowspan":0,"hyperlinkedDoc":"","isLastCell":true,"value":"1 April 2015"}}]');
$scope.documentsList.push(JSON.parse('[{"idANX":1,"pathANX":"Product Overview.pdf","isHeaderANX":0,"isExcludedANX":0,"isNotHyperlinkedANX":0,"isSubHeaderANX":0,"HyperlinkedColumnANX":1,"TabNo":{"colspan":0,"rowspan":0,"hyperlinkedDoc":"","isLastCell":false,"value":"1"},"Document":{"colspan":0,"rowspan":0,"hyperlinkedDoc":"Product Overview.pdf","isLastCell":false,"value":"Product Overview"},"PageNumber":{"colspan":0,"rowspan":0,"hyperlinkedDoc":"","isLastCell":false,"value":"1 - 2"}},{"idANX":2,"pathANX":"Spec.pdf","isHeaderANX":0,"isExcludedANX":0,"isNotHyperlinkedANX":0,"isSubHeaderANX":0,"HyperlinkedColumnANX":1,"TabNo":{"colspan":0,"rowspan":0,"hyperlinkedDoc":"","isLastCell":false,"value":"2"},"Document":{"colspan":0,"rowspan":0,"hyperlinkedDoc":"Spec.pdf","isLastCell":false,"value":"Spec"},"PageNumber":{"colspan":0,"rowspan":0,"hyperlinkedDoc":"","isLastCell":false,"value":"3 - 4"}}]');
$scope.getNumberArray = function (num) {
var n = new Array(num);
var i;
for (i = 0; i < num; i++) {
n[i] = i;
}
return n;
}
$scope.rowClass = function (row) {
if (row.isSubHeaderANX == 1) {
return 'subheader';
} else if (row.isHeaderANX == 1) {
return 'header';
}
else { return 'doctablerow'; }
}
这些在C ++中很危险,因为它们的初始化和销毁顺序是不可预测的,容易出错。
Python没有静态存储持续时间的变量。它根本没有存储持续时间。它具有(模块)全局变量和静态变量,但它们的初始化顺序比C ++中的定义要精确得多。除非你有循环导入,否则不太可能发生C ++问题,即使这样,循环导入也是问题,而不是全局变量或静态变量。
也就是说,可变的全局状态,例如你在程序过程中发生变异的全局变量或静态变量,对于language-independent reasons是不好的。这与C ++具有静态存储持续时间的问题完全不同。你不打算变异的全局变量和静态变量通常很好。