0

so, I am building a python project with some oop, but when I try to run my program, it gives me this:

TypeError: take_query() missing 1 required positional argument: 'type'

but I have given the program the type argument, so, I removed the type argument and tried it again, and this time it gave me this:

TypeError: take_query() missing 2 required positional arguments: 'self' and 'type'

that means python is taking self as an argument, how can I fix that? also, I am doing this in vs code, the code for the class is :

def take_query(self,type):
    # Running forever until user says goodbye (or similar)
    while (True):
        # This class is responsible for converting audio into text
        r = sr.Recognizer()

        # using the Microphone module from sr that will listen for a query
        with sr.Microphone() as source:
            # Energy threshold of 300 is recommended by SpeechRecognition's documentation (loudness of the file)
            r.energy_threshold = 300

            # How long it will wait after the user stops talking to consider the end of a sentence
            r.pause_threshold = 0.7
            audio = r.listen(source)

            # Now trying to use Google Recognizer function that uses Google’s free web search API
            # This try except block will check if the words are recognized, else an exception will be handled
            try:
                    print("Processing...")

                    instruction = r.recognize_google(audio, language="en-in")

                    print("You said: ", instruction)
            except:
                    pass
pert
  • 65
  • 7
  • In python any method can be called in procedural style, and the first argument is `self`. If you are in a class, you must call `self.take_query()` (or `take_query(self)`) instead of just `take_query()`. – Dávid Horváth Jan 20 '21 at 06:49
  • so, is `self ` automatically put in? – pert Jan 20 '21 at 06:51
  • 2
    You should include the code for your class. – PacketLoss Jan 20 '21 at 06:51
  • 1
    Please provide the code of the class with the method you're calling and an example of how you actually call it. – Grismar Jan 20 '21 at 06:54
  • 1
    You just added the code for this particular method `take_query`, but you haven't added the class itself or how you're calling this method. – Arya McCarthy Jan 20 '21 at 06:58
  • i added it in the code before, which i did not include here – pert Jan 20 '21 at 06:59
  • Because `self` *is* a parameter – juanpa.arrivillaga Jan 20 '21 at 06:59
  • how do you call the `take_query`? – HW Siew Jan 20 '21 at 07:03
  • i imported it in from another `.py` file – pert Jan 20 '21 at 07:04
  • I wonder, why would you input `self` and `type` parameters and not use them in your funcion? Are you sure those parameters are necessary? Also, do this function need to be a class function? It's not mutating any internal values so maybe this should be a standalone function. – Susensio Jan 20 '21 at 07:56
  • In addition, you should not use `type` as a function parameter, as you are masking out a keyword. Take a look here https://stackoverflow.com/questions/10568087/is-it-safe-to-use-the-python-word-type-in-my-code – Susensio Jan 20 '21 at 07:59

1 Answers1

0

In python, basically for calling a function of class, you have first create an object of that class and then inside the function, self is the keyword used to refer to the current object and hence you have to have it as a requried parameter/argument. So, you have call the method take_query as self.take_query() or take_query() inside the class definition. Now, outside the class these are steps to be followed.

object1 = Your_Class_Name(Your_Arguments)
object1.take_query() or take_query(object1)

If your method has other arguments you can add it along with your instance.