1

On python 3.3 using 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()

Error

TypeError: ratio() takes 0 positional arguments but 1 was given
thefourtheye
  • 221,210
  • 51
  • 432
  • 478
jhon.smith
  • 1,865
  • 5
  • 25
  • 44
  • 2
    Related question: [Why accessing to class variable from within the class needs “self.” in Python?](http://stackoverflow.com/q/13652006/510937) – Bakuriu Jan 23 '14 at 13:15

3 Answers3

7

When you say

mygear.ratio()

python will internally invoke the function like this

ratio(mygear)

But as per the definition of that function,

def ratio () :

it doesn't accept any input parameters. Change it to accept the current object, like this

def ratio(self):
thefourtheye
  • 221,210
  • 51
  • 432
  • 478
2
def ratio(self):

You need to put self inside the methods

petabyte
  • 1,441
  • 4
  • 14
  • 30
2

All instance methods in Python need to take the self argument.

def ratio(self):
Daniel Roseman
  • 567,968
  • 59
  • 825
  • 842