Python - Timeit within a class

if you're willing to consider alternatives to timeit, i recently found the stopwatch timer utility which might be useful in your case. it's really simple and intuitive, too:

import stopwatch

class TimedClass():

    def __init__(self):
        t = stopwatch.Timer()
        # do stuff here
        t.stop()
        print t.elapsed

Why do you want the timing inside the class being timed itself? If you take the timing out of the class, you can just pass a reference. I.e.

import timeit

class TimedClass():
    def __init__(self):
        self.x = 13
        self.y = 15

    def square(self, _x, _y):
        print _x**_y

myTimedClass = TimedClass()
timeit.Timer(myTImedClass.square).timeit()

(of course the class itself is redundant, I assume you have a complexer use-case where a simple method is not sufficient).

In general, just pass a callable that has all setup contained/configured. If you want to pass strings to be timed they should contain all necessary setup inside them, i.e.

timeit.Timer("[str(x) for x in range(100)]").timeit()

If you really, really need the timing inside the class, wrap the call in a local method, i.e.

def __init__(self, ..):
    def timewrapper():
        return self.multiply(self.x, self.y)

    timeit.Timer(timewrapper)

To address your initial error, you can use timeit within a class with parameters like this:

 t = timeit.Timer(lambda: self.square(self.x, self.y)).timeit()