0

I am kind of new to python and the concept of threading, so I have had some questions regarding it. I am trying to run a thread inside a class, but when I write the target with the "self.function()" form it doesnt work. If I leave the function without the "self." for example, "function()" then I get an error in the terminal telling me that I need to have the "self." in front of the function's name. Here is a simlified version of the code:

class pong:
    def run_game(self):
        while True:
            self.bounce()
            self.run_ball()
            #There is more functions in this loop but it isnt important
    #Then ive got another loop that i need to be running while the game is active:
    def run_level():
        while True;
            self.level()

    t1 = threading.Thread(self.run_game)
    #This line of code up here is the problematic one. You see if I write it as it is it doesn't work, I get a self not defied error. If I write it like this ...Thread(run_game), then it looks for the self in front of the function name.

I really hope this will reach someone who knows about python very well and I whill get an answer. Thank you!

I tried threading as shown on the latter and it didn't work.

Ylli K
  • 1
  • You are defining a static member like that. Use a constructor and assign `self.t1` instead of `t1`. Also, `run_level` must have a `self` parameter if it wants to call something else using `self.`. `while True;` should be `while True:`. Classes should have CamelCase naming. Oh dear ... please come back after doing a tutorial or so. – Thomas Weller Jul 06 '23 at 06:17
  • @ThomasWeller Thank you very much for your reply! I will try out what you just said. Also I made a mistake when writing fast that's why I wrote "while True;" and not not "while True:". I do write class names in CamelCase format but I made a mistake while writing fast. And of course I know that a function in a class has got (self) as a parameter. I just made mistakes, or forgot a line of code while writing, please forgive me of any mistakes here, they arent present on the actual code :) – Ylli K Jul 06 '23 at 06:37
  • Okay. Just be aware that you confuse people who are trying to help you. It's hard to judge about your level of knowledge when there are such basic mistakes in the code. Please put as much effort in your [mre]s as you put in your real code. You'll get much better answers and probably more upvotes. – Thomas Weller Jul 06 '23 at 07:17
  • @ThomasWeller thank you for helping me out. Could you please tell me in a more simplified language what a constructor is? And how they will help me assign self.t1 instead of t1? – Ylli K Jul 06 '23 at 12:36
  • 1
    Does this answer your question? [Python constructors and \_\_init\_\_](https://stackoverflow.com/questions/8985806/python-constructors-and-init) – Thomas Weller Jul 06 '23 at 17:26
  • @ThomasWeller Thank you so much for your help! I am now able to multithread my program! – Ylli K Jul 07 '23 at 05:59

0 Answers0