我想在函数中使用在init中定义的值。即
class foo
def __init__(self, thingone):
self.thingone = thingone
def printone():
print(self.thingone)
x = foo(1, 2)
x.printone()
但是会引发错误,表明'自我'不存在
class foo
def __init__(self, thingone):
self.thingone = thingone
def printone(self):
print(self.thingone)
x = foo(1, 2)
x.printone()
并引发错误,该错误未将'self'赋予该函数。
我如何访问这些变量?
init
方法中接受2个参数。class foo:
def __init__(self, thingone, thingtwo):
self.thingone = thingone
def printone(self):
print(self.thingone)
x = foo(1, 2)
x.printone()
class foo:
def __init__(self, *thingone):
self.thingone = thingone
def printone(self):
print(self.thingone)
x = foo(1, 2)
x.printone()
# (1, 2)