0

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()
4
  • 2
    what exaclty is "Ecole"? I can't see where you define it. Commented Jun 12, 2018 at 16:13
  • Can you tell me, on which line the error is raising? Commented Jun 12, 2018 at 16:15
  • 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. Commented Jun 12, 2018 at 16:15
  • "Ecole" is French for "School"; I changed the identifier. Commented Jun 12, 2018 at 16:18

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.