I'm trying to use a class, but I have this error:
TypeError: search() missing 1 required positional argument: 'self'
This is my code:
class School:
def search(self):
... # do some stuff
harva = School()
School.search()
Your final line is incorrect: you must (il faut que vous) invoke an instance method by calling it with an instance, not with the class:
harva.search()
When you call the method as a class method, it needs the instance:
School.search(harva)
However, you should use the first version.
In the future, don't write so much code at once. You should try to invoke the method before you have written all of that.
I think you mean harva.search()
instead of School.search()
. You defined search as an instance method. You must call it on an instance of school, and not on School class itself.
In object-oriented programming...the basic fundamental element is an object and a class along with its member variables
& member functions
can only be accessed through an instance of that class ie again object .
class School():
def search(self):
...#some code
harva = School()
harva.search()
You can access a function inside a class without creating an instance of that class. Such functions are called as static functions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.