0

I am facing some error while using classes in python 2.7

My class definition is:

class Timer(object):

    def __init__(self):
        self.msg = ""

    def start(self,msg):
        self.msg = msg
        self.start = time.time()

    def stop(self):
        t = time.time() - self.start
        return self.msg, " => ", t, 'seconds'

On executing the following code.

timer = Timer()
timer.start("Function 1")
Some code
timer.stop()

timer.start('Function 2')
some code
timer.stop()

I am getting following error:

Function 1 => 0.01 seconds
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object is not callable

For the first call it worked as desired but for the second call, it gave an error. I am unable to figure out the cause of the error.

10
  • 2
    t = time.time() - self.start looks wrong. self.start is a function. Commented May 30, 2017 at 12:55
  • Yeah you are right thanks Commented May 30, 2017 at 12:56
  • self.start = time.time() is also wrong Commented May 30, 2017 at 12:57
  • 1
    @sv_jan5 also, the question was a very valid one, +1 for it, I liked to find the issue haha Commented May 30, 2017 at 13:32
  • 1
    @sv_jan5 indeed! I actually edited again my answer so it can be now finally right and complete Commented May 30, 2017 at 13:44

3 Answers 3

3

When you write self.start = time.time(), you replace the function start() with a variable named start, which has a float value. The next time you write timer.start(), start is a float, and you are trying to call it as a function. Just replace the name self.start with something else.

Sign up to request clarification or add additional context in comments.

Comments

1

I think the problem is that you are using the same name for a method and for an attribute.

I would refactor it like this:

class Timer(object):

    def __init__(self):
        self.msg = ""
        self.start_time = None #I prefer to declare it but you can avoid this

    def start(self,msg):
        self.msg = msg
        self.start_time = time.time()

    def stop(self):
        t = time.time() - self.start_time
        return self.msg, " => ", t, 'seconds'

Comments

0

I think now I understood your question and error, the following correct your issue:

import time

class Timer(object):

  def __init__(self):
      self.msg = ""

  def start(self,msg):
      self.msg = msg
      self.start = time.time() # Here, your method name is the same has the variable name

If you rename the variable name, you have to remember to call first the start method so the start_value variable exists:

import time

class Timer(object):

  def __init__(self):
      self.msg = ""

  def start(self,msg):
      self.msg = msg
      self.start_value = time.time() #Is you change the variable name, the problem is solved

  def stop(self):
      t = time.time() - self.start_value
      return self.msg, " => ", t, 'seconds'


a = Timer()
a.start("first call")
print a.stop()

>> ('first call', ' => ', 1.9073486328125e-06, 'seconds')

but if you don't call the method and just do:

a = Timer()
# a.start("first call") without it
print a.stop()

the variable start will never exist, wich throws to you the error:

AttributeError: 'Timer' object has no attribute 'start_value'

I hope that helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.