Skip to main content
 首页 » 编程设计

python之0 位置参数,但给出了一个,但为什么

2025年05月04日16lori

在 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() 

错误

TypeError: ratio() takes 0 positional arguments but 1 was given 

请您参考如下方法:

当你说

mygear.ratio() 

python 会像这样在内部调用函数

ratio(mygear) 

但是根据那个函数的定义,

def ratio () : 

它不接受任何输入参数。改变它以接受当前对象,像这样

def ratio(self):