rab*_*tam 5 python static-methods
从类内部调用静态方法(包含静态方法)时,可以通过以下方式完成:
Class.method()或self.method()有
什么区别?
每个用户的独特用例是什么?
class TestStatic(object):
@staticmethod
def do_something():
print 'I am static'
def use_me(self):
self.do_something() # 1st way
TestStatic.do_something() # 2nd way
t = TestStatic()
t.use_me()
Run Code Online (Sandbox Code Playgroud)
版画
I am static
I am static
Run Code Online (Sandbox Code Playgroud)
通过使用TestStatic.do_something()你绕过子类的任何覆盖:
class SubclassStatic(TestStatic):
@staticmethod
def do_something():
print 'I am the subclass'
s = SubclassStatic()
s.use_me()
Run Code Online (Sandbox Code Playgroud)
将打印
I am the subclass
I am static
Run Code Online (Sandbox Code Playgroud)
这可能是你想要的,也可能不是.选择最符合您期望的方法.
| 归档时间: |
|
| 查看次数: |
801 次 |
| 最近记录: |