Code itself:
class tasksformath:
DEF TASKNUMBER1 (SELF):
Print ("\ n trip drifted at a speed of 110 km / h. With the same speed, he drove 6 hours. \ No distance overcame the train for these 6 hours?")
While True:
NOW_EXECUTE_NUMBER = 1.
Print (f "Task number {NOW_EXECUTE_NUMBER}")
TasksFormath.TaskNumber1 ()
And Error:
file "C: \ users \ FleshStinger \ Desktop \ tasks.", Line 10, In & LT; Module & gt;
TasksFormath.TaskNumber1 ()
TypeRorror: TaskNumber1 () Missing 1 Required Postal Argument: 'Self'
What I wanted to do: I want to have a function from class TasksFormath “tasknumber1 () ,
But he asks the argument. I added the argument self but gives an error. What to do?
Answer 1, Authority 100%
First you decide: a static method or object method?
Static
static can be called the type (or objects such as objects), as:
tasksformath.task_number_1 ()
Static methods No SELF
argument and they need to specify the @Staticmethod decorator
:
class tasksformath:
@Staticmethod.
DEF Task_Number_1 ():
...
object method
The object method can be called exclusively objects:
tasks = tasksformath ()
tasks.task_number_1 ()
For these methods, the argument Self
:
class tasksformath:
DEF Task_Number_1 (Self):
...
class method
The class method is very similar to the static method: can be called the type (or objects such as objects), as:
tasksformath.task_number_1 ()
But there is one chip – mandatory transmission of the CLS
argument to the class methods and the specification of the decorator @ClassMethod
:
class tasksformath:
@classmethod.
DEF Task_Number_1 (CLS):
...
This will allow in the class methods when handling CLS
Call fields and methods of class
Answer 2, Authority 50%
Because tasknumber1
is not classmethod
, then it is necessary to create a class object in which this method is defined.
Just adding two round brackets into your code fragment, we get an acceptable result:
class tasksformath:
DEF TASKNUMBER1 (SELF):
Print ("\ n trip drifted at a speed of 110 km / h. With the same speed, he drove 6 hours. \ No distance overcame the train for these 6 hours?")
While True:
NOW_EXECUTE_NUMBER = 1.
Print (f "Task number {NOW_EXECUTE_NUMBER}")
TasksFormath (). TaskNumber1 ()
Please note that the cycle is endless.
If you do not want to create an instance of a class, then use the @ClassMethod
:
class tasksformath:
@classmethod.
DEF TASKNUMBER1 (CLS):
Print ("\ n trip drifted at a speed of 110 km / h. With the same speed, he drove 6 hours. \ No distance overcame the train for these 6 hours?")
While True:
NOW_EXECUTE_NUMBER = 1.
Print (f "Task number {NOW_EXECUTE_NUMBER}")
TasksFormath (). TaskNumber1 ()
Answer 3
Simply remove self
from the function arguments, if you do not want it to be a class instance method, and you want it to be a static method that can be called using the class name:
class TasksForMath:
def TaskNumber1 ():
print ("\ nThe train traveled at 110 km / h. It traveled at the same speed for 6 hours. \ nWhat distance did the train cover in those 6 hours?")
now_execute_number = 1
print (f "Task number {now_execute_number}")
TasksForMath.TaskNumber1 ()
I removed the loop because it is infinite.
Conclusion:
Problem number 1
The train was traveling at a speed of 110 km / h. He drove at the same speed for 6 hours.
How far has the train covered in these 6 hours?