【发布时间】:2019-11-04 07:29:44
【问题描述】:
以下代码 sn-p 在 python2 中给出错误但在 python3 中没有错误
class Steps(object):
def step(self, msg="Default"):
if not hasattr(self, "funky_attr"):
print('No attr')
print(self)
print(msg)
class FirstTest(Steps):
@classmethod
def test_setup(cls):
cls.step("This is the message")
if __name__ == '__main__':
C = FirstTest()
C.test_setup()
使用 python 2 会产生错误:
TypeError:未绑定的方法 step() 必须使用 CifsTest 实例调用 作为第一个参数(取而代之的是 str 实例)
虽然使用 python 3 运行良好:
无属性
这是消息 # 注意这里的'self'是str
默认
这是正确的行为吗?
Python3 允许从类方法调用实例方法?
【问题讨论】:
标签: python python-3.x python-2.7 class-method