[英]How to def a function inside a Class
So guys, sorry if this question posted again.所以伙计们,对不起,如果这个问题再次发布。 i dont know, why convert_int() is undefined even i've defined it.
我不知道,为什么即使我已经定义了 convert_int() 也是未定义的。 please reply this if you can answer.
如果你能回答,请回复这个。 Thank you guys:)
感谢你们:)
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())
You have used convert_int
as for an instance method in the class.您已将
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.