2010-06-15 66 views

回答

4

如果你不想让它成为Table类的成员,你可以把它放到utilities模块中。

11

使它成为一个静态函数...

  • 添加@staticmethod装饰
  • 不包括 '自我' 作为第一个参数

你的定义是:

@staticmethod 
def rotateMatrixClockwise(): 
    # enter code here... 

这将使它可以调用无论你通过调用进口“表”:

table.rotateMatrixClockwise() 

的装饰,只需要告诉蟒蛇没有隐含的第一个参数的预期。如果你想让方法定义像C#/ Java那样自我总是隐含的,你也可以使用'@classmethod'装饰器。

Here's the documentation for this coming directly from the python manual.

注:我建议你使用的工具类只有在他们的代码不能直接连接到模块,因为他们一般违反了OOP的“Single Responsibility Principle”。它几乎是总是最好将类的功能作为方法/成员绑定到类。

相关问题