Python Forum
Return boolean from recursive class method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return boolean from recursive class method
#1
I have the following class which stores bit strings in a tree structure. The exists() method should return if the queried string is matched in the tree or not. However, I can't get my method return a boolean result. Can someone help?

class Node:
    def __init__(self, data):
        self.left = None
        self.right = None
        self.data = data
    def insert(self, data):
        if self.data and data != '':
            if data[0] == '0':
                if self.left is None:
                    self.left = Node(data[0])
                self.left.insert(data[1:])
            elif data[0] == '1':
                if self.right is None:
                    self.right = Node(data[0])
                self.right.insert(data[1:])
    def exists(self, data):
        if data == '':
            print("Match")
            return True
        if data[0] == '0':
            if self.left is None:
                print("No match")
                return False
            else:
                self.left.exists(data[1:])
        elif data[0] == '1':
            if self.right is None:
                print("No match")
                return False
            else:
                self.right.exists(data[1:])

b = '1001'
r = Node('root')
r.insert(b)
r.exists(b)
r.exists('1111')
Reply
#2
The else blocks on lines 25 and 31 don't seem to have a return. What would you expect to return if you reach those lines?
Reply
#3
(Jul-13-2020, 04:13 AM)bowlofred Wrote: The else blocks on lines 25 and 31 don't seem to have a return. What would you expect to return if you reach those lines?

These are the recursive method calls
Reply
#4
Your recursive calls don't have return statements, so those branches end up returning None.
Reply
#5
(Jul-13-2020, 04:18 AM)ndc85430 Wrote: Your recursive calls don't have return statements, so those branches end up returning None.

Can you help on how to fix this?
Reply
#6
It was implied: add return statements to those lines.
Reply
#7
(Jul-13-2020, 04:25 AM)ndc85430 Wrote: It was implied: add return statements to those lines.

Got it working, thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  super() and order of running method in class inheritance akbarza 7 594 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  function return boolean based on GPIO pin reading caslor 2 1,129 Feb-04-2023, 12:30 PM
Last Post: caslor
  Using one child class method in another child class garynewport 5 1,483 Jan-11-2023, 06:07 PM
Last Post: garynewport
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,267 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,379 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,678 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,036 Jan-09-2022, 02:39 AM
Last Post: Python84
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,657 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  anonymous method in a class Skaperen 8 3,475 May-23-2021, 11:17 PM
Last Post: Skaperen
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,189 Jan-17-2021, 03:02 AM
Last Post: Jeremy7

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020