0
votes

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()
3
what exaclty is "Ecole"? I can't see where you define it. - diegoiva
Can you tell me, on which line the error is raising? - Letsintegreat
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Your posted code fails to run: Ecole is not defined; it's named as a class. - Prune
"Ecole" is French for "School"; I changed the identifier. - Prune

3 Answers

0
votes

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.

1
votes

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.

0
votes

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 i.e 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.