如何使此代码在3中工作?
请注意,我并不是在字符串实例级别询问"foo".upper()
。
import string
try:
print("string module, upper function:")
print(string.upper)
foo = string.upper("Foo")
print("foo:%s" % (foo))
except (Exception,) as e:
raise
输出2:
string module, upper function:
<function upper at 0x10baad848>
foo:FOO
输出3:
string module, upper function:
Traceback (most recent call last):
File "dummytst223.py", line 70, in <module>
test_string_upper()
File "dummytst223.py", line 63, in test_string_upper
print(string.upper)
AttributeError: module 'string' has no attribute 'upper'
help(string)
也不是很有帮助。据我所知,剩下的唯一功能是string.capwords
。
注意:有点黑,但这是我的短期解决方法。
import string
try:
_ = string.upper
except (AttributeError,) as e:
def upper(s):
return s.upper()
string.upper = upper
答案 0 :(得分:1)
您描述的所有this.reset
模块级函数都已在Python 3中删除。Python 2 string
module documentation包含以下注释:
尽管这些功能要到Python 3才会被删除,您应该将其视为已弃用。
如果您在Python 2中拥有import _ from 'loadash'
class MyApp extends Component {
...
componentWillReceiveProps(nextProps) {
const {data} = nextProps;
// Invoke cloneDeep to get a clone of data that is also a unique
// reference
this.reset = _.cloneDeep(data);
this.setState({
data
});
}
resetData(){
// Remember to clone reset data each time you reset data, so that
// data is a reference to a new copy of this.reset, rather than
// the reset data you stored when componentWillReceiveProps was
// called
this.setState({
data: _.cloneDeep(this.reset)
});
}
...
}
,则需要在Python 3中将其转换为string
。