我正在创建一个具有许多方法的类,作为步骤之一,我想更改一个args以使用与该类不同的方法来调用新版本。
我确定这很简单,但我无法弄清楚哪里出了问题。
为简洁起见,我尝试从我实际编写的代码中举一个简单的例子。
class Double:
def __init__(self, x):
self.x = x
def double(self):
self.x = self.x * 2
# change the x item to x*2
return self.x
def return1(self, xg=x):
# if xg isn’t defined by user, this method should return
# the new version of x created above
return xg
我也希望对我的课堂编码有其他反馈。
答案 0 :(得分:3)
目前尚不清楚您要完成什么,并且可能会有更好的设计,但是要回答您的问题,可以使用以下惯用法:
def return1(self, xg=None):
return xg if xg is not None else self.x