如何让这段代码在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:
foo:FOO
输出3:string module, upper function:
Traceback (most recent call last):
File "dummytst223.py", line 70, in
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。
注意:有点hacky,但这是我的短期解决方法。import string
try:
_ = string.upper
except (AttributeError,) as e:
def upper(s):
return s.upper()
string.upper = upper