没有定义变量名时如何调用方法?
How do I call a method when I have not defined a variable name?
我正在尝试弄清楚如何在未命名变量上调用方法,以便我能够在不必创建对象的情况下测试我的方法。我想你在 class 上调用它?但是我不确定该怎么做..
class Base:
def __init__(self):
self.x = 'Base'
def foo(self):
return 'ball'
def __str__(self):
return self.x
我希望能够在另一个 class 中说 print(Base.foo()) 而无需创建新对象
我想你想要一个 class 方法。
class Base:
def __init__(self):
self.x = 'Base'
@classmethod
def foo(cls):
return 'ball'
def __str__(self):
return self.x
我正在尝试弄清楚如何在未命名变量上调用方法,以便我能够在不必创建对象的情况下测试我的方法。我想你在 class 上调用它?但是我不确定该怎么做..
class Base:
def __init__(self):
self.x = 'Base'
def foo(self):
return 'ball'
def __str__(self):
return self.x
我希望能够在另一个 class 中说 print(Base.foo()) 而无需创建新对象
我想你想要一个 class 方法。
class Base:
def __init__(self):
self.x = 'Base'
@classmethod
def foo(cls):
return 'ball'
def __str__(self):
return self.x