r/PythonLearning • • Aug 14 '25

RECURSION CONFUSED ME ABOUT init

Post image

What is wrong and why is init not working

0 Upvotes

10 comments sorted by

11

u/Ender_Locke Aug 14 '25

i love reading sideways text

7

u/MysticClimber1496 Aug 14 '25

Init is called when you create the object, at that time it doesn’t exist because the check function hasn’t been called, this is also not recursive

I.e. init is never called

6

u/ilan1k1 Aug 14 '25

What are you trying to do here? What are you attempting to learn?

5

u/Adsilom Aug 14 '25

I have no idea what you are trying to achieve. You seem confused about a lot of things? Do you understand what classes are? If not, don't use them, and focus on recursion, if that's what you are interested in.

3

u/ConcreteExist Aug 14 '25

Why is __init__ inside the check method? That's your problem right there.

There really shouldn't even be an __init__ function at all, you could just get rid of the def __init__ lines and you'd have valid code.

3

u/DevRetroGames Aug 14 '25
class TestForTheory:
  def __init__(self, n: int, m: int):
    self.n = n
    self.m = m
    
  def _nGreaterM(self) -> None:
    print(f"{self.n} is greater that {self.m}")
  
  def _nLessM(self) -> None:
    print(f"{self.n} is ledd than {self.m}")
    
  def check(self):
    self._nGreaterM() if self.n > self.m else self._nLessM()
    
p1 = TestForTheory(3, 4)
p1.check()

2

u/IceMan420_ Aug 15 '25

Here allow me 🤓:

class TestForTheory:
    def __init__(self, n, m):
        self.n = n
        self.m = m

    def check(self):
        if self.n > self.m:
            print(f"{self.n} is greater than {self.m}")
        elif self.n < self.m:
            print(f"{self.n} is less than {self.m}")
        else:
            print(f"{self.n} is equal to {self.m}")


p1 = TestForTheory(100, 100)
p1.check()
p2 = TestForTheory(100, 200)
p2.check()
p3 = TestForTheory(300, 200)
p3.check()

100 is equal to 100
100 is less than 200
300 is greater than 200

1

u/shlepky Aug 14 '25

Construct your class (create the init dunder method) with n and m as necessary parameters. Then call the check method which does your wanted initialization.

1

u/SCD_minecraft Aug 15 '25

Order of things is about

__new__

(Object gets created) __init__

Everything else

You call "everything else" before defining __init__, so it misses its turn