在python 3.3上使用Ipython
class Gear:
def __init__(self,chainring,cog):
self.chainring = chainring
self.cog = cog
def ratio () :
ratio = self.chainring/self.cog
return ratio
mygear = Gear(52,11)
mygear.ratio()
Run Code Online (Sandbox Code Playgroud)
错误
TypeError: ratio() takes 0 positional arguments but 1 was given
Run Code Online (Sandbox Code Playgroud)
当你说
mygear.ratio()
Run Code Online (Sandbox Code Playgroud)
python将在内部调用这样的函数
ratio(mygear)
Run Code Online (Sandbox Code Playgroud)
但根据该功能的定义,
def ratio () :
Run Code Online (Sandbox Code Playgroud)
它不接受任何输入参数.将其更改为接受当前对象,就像这样
def ratio(self):
Run Code Online (Sandbox Code Playgroud)