[英]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.