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
  • 233,700
  • 52
  • 457
  • 497
jhon.smith
  • 1,963
  • 6
  • 30
  • 56
  • 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
  • 233,700
  • 52
  • 457
  • 497
2
def ratio(self):

You need to put self inside the methods

petabyte
  • 1,487
  • 4
  • 15
  • 31
2

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

def ratio(self):
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895