![]() |
Confusion about TypeError and 'self' - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Confusion about TypeError and 'self' (/thread-26381.html) |
Confusion about TypeError and 'self' - Muzzrooms - Apr-29-2020 Hey everyone, this is my first post so I hope this is clear. I am working on some code where the user is asked to select from a comboBox. This problem I have is the code is suggesting I need to include the self argument however when I include it, it still errors out. I've gone around in circles for a couple of days now. Your insights would be greatly appreciated. WITHOUT SELF ARGUMENT def play(self): guess = str(self.ui.comboBox.currentText()) return guess if play() == comp_num: winmessage() else: losemessage()And this is the Error WITH SELF ARGUMENTdef play(self): guess = str(self.ui.comboBox.currentText()) return guess if play(self) == comp_num: winmessage() else: losemessage()And this is the error I receive if I include the self argument. Your help is greatly appreciated;-Muzzrooms RE: Confusion about TypeError and 'self' - deanhystad - Apr-29-2020 Showing all your code would be helpful. My guess is you have written a class and play is one of the methods of the class. To call a method the correct syntax is object.method(arguments). Python uses the object to find out where to look for method. It then calls the method like this "method(object, arguments). Somewhere you are creating an instance of you class and hopfully keeping the returned object in a variable. Something like this: myobject = MyClass(maybe, some, args) To call the play method you would type: myobject.play() RE: Confusion about TypeError and 'self' - stullis - Apr-29-2020 def play(self): guess = str(self.ui.comboBox.currentText()) return guess if self.play() == comp_num: # Self may need to be changed to the name of the object depending on usage. winmessage() else: losemessage()Because play() is evidently a method, it must be prefaced by a reference to the instance running the method. When called within a method of the class, it must be prefaced with "self.". Otherwise, it lacks an argument for that parameter. |