TypeError: start () missing 1 required positional argument: 'self' when trying to use the chronometer module

1

I installed the chronometer module 1.0 for python 3.4: chronometer module

I tried to use the start () attribute of the Chronometer method, but it gives an error: it requires a 'self' argument

>>>from chronometer import Chronometer as crmt
>>>crmt.start()
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    crmt.start()
TypeError: start() missing 1 required positional argument: 'self'
>>>crmt.start(self)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    crmt.start(self)
NameError: name 'self' is not defined

Can someone explain how to solve this problem and why the chronometer is working correctly, and what is the code for writing a functional chronometer?

    
asked by anonymous 29.06.2016 / 21:15

1 answer

1

When you do ...

from chronometer import Chronometer as crmt

... you are importing the Chronometer class from module chronometer into the current namespace and calling it crmt .

Note that chronometer.Chronometer is a class (that is, a definition) and not an object. When you do crmt.start() , the Python interpreter will look for the .start() method in what it thinks is an object of type Chronometer , will find , since start() , in fact , is set to Chronometer , will try to execute, but will not succeed because parameter self has not been passed.

self is a parameter that gets the object itself, which is passed automatically when you do something like this: objeto.metodo() . It is similar to this of C ++ and Java, but Python has chosen to make self an explicit parameter.

What to do, then? What you probably want is the following:

from chronometer import Chronometer
crmt = Chronometer()
crmt.start()

The second line creates a copy of Chronometer , which will make the third line run smoothly.

    
03.07.2016 / 12:41