[英]How to def a function inside a Class
所以伙计们,对不起,如果这个问题再次发布。 我不知道,为什么即使我已经定义了 convert_int() 也是未定义的。 如果你能回答,请回复这个。 感谢你们:)
class Abc(object):
def __init__(self,words):
self.words = words
def convert_int(s): #here the problem, @staticmethod can't solve this
try:
return int(s)
except ValueError:
return s
def test(self):
words = self.words
words = convert_int(words)
return words
f = Abc("12345")
print(f.test())
您已将convert_int
用作 class 中的实例方法。
class Abc(object):
def __init__(self,words):
self.words = words
def convert_int(self, s): #updated
try:
return int(s)
except ValueError:
return s
def test(self):
words = self.words
words = self.convert_int(words) #updated
return words
f = Abc("12345")
print(f.test())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.