Typeerror: start() Missing 1 required positional argument: 'self' when trying to use the Chronometer module

Asked

Viewed 1,161 times

2

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

I tried to use the start() attribute of the Chronometer method, but gives an error: 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 to me how to solve this problem and get the timer to work correctly, and what is the code to write a functional stopwatch?

1 answer

1


When you do...

from chronometer import Chronometer as crmt

...you are importing the class Chronometer module chronometer to the current namespace and nicknamed it crmt.

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

self is a parameter that receives the object itself, which is passed automatically when you do something like this: objeto.metodo(). Is similar to this C++ and Java, but Python chose 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.