Python Forum
Python returns generator instead of None
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python returns generator instead of None
#1
I have no clue why this is occurring. My class function isn't returning None regardless of how I write my code. Here's my search function below:

    def search(self, box):
        if len(self.truck.boxes) == 0:
            return None
        start = 0
        end = len(self.truck.boxes) - 1
        while end - start > 1:
            mid = (start + end) // 2
            if self.truck.boxes[end] < self.truck.boxes[start]:
                if self.truck.boxes[mid].volume() <= box.volume() and box.volume() <= self.truck.boxes[end].volume(): ##indent is okay?
                    start = mid
                elif self.truck.boxes[mid].volume() <= box.volume() and box.volume() >= self.truck.boxes[end].volume():
                    end = mid
                elif self.truck.boxes[mid].volume() > box.volume() and box.volume() >= self.truck.boxes[start].volume():
                    end = mid
                elif self.truck.boxes[mid].volume() > box.volume() and box.volume() <= self.truck.boxes[end].volume():
                    start = mid
            elif self.truck.boxes[end] > self.truck.boxes[start]:
                if self.truck.boxes[mid].volume() <= box.volume():
                    start = mid
                else:  #self.truck.boxes[mid].volume() > box.volume()
                    end = mid
            
        right = None
        if self.truck.boxes[start].volume() != box.volume() and self.truck.boxes[end] != box.volume():
            return None
        elif self.truck.boxes[start].volume() == box.volume():
            right = start
        elif self.truck.boxes[end].volume() == box.volume():
            right = end

        start = 0
        while right - start > 1:
            mid = (right + start) // 2
            if self.truck.boxes[mid].volume() == self.truck.boxes[right].volume():
                right = mid
            else:
                start = mid
                
        begin = 0
        if self.truck.boxes[start].volume() == box.volume():
            begin = start
        else:  # self.truck.boxes[right].volume() == box.volume():
            begin = right
            
        while self.truck.boxes[begin].volume() == box.volume():
            yield self.truck.boxes[begin]
            begin += 1
Particularly this snippet of the code here:

right = None
    if self.truck.boxes[start].volume() != box.volume() and self.truck.boxes[end] != box.volume():
        return None
    elif self.truck.boxes[start].volume() == box.volume():
        right = start
    elif self.truck.boxes[end].volume() == box.volume():
        right = end
I can assure (through my own testing) that the code should be returning None here when I input a box object whose volume is not found in the OrganizedTruck's list of boxes.

Just before the above snippet of code, self.truck.boxes[start].volume() and self.truck.boxes[end].volume() are NOT equal to the volume of the input box (I'm 99% sure this is true, I debugged via print functions), and therefore the first condition should be satisfied and the code should return None, right?

However, when I do run my code, my function returns some generator instead: <generator object OrganizedTruck.search at 0x00000272D1424258>

Why isn't my function returning None? I know for sure that that condition should be triggered, so I have no explanation for this other than Python is ignoring the condition completely...
Reply


Messages In This Thread
Python returns generator instead of None - by Tawnwen - Mar-08-2018, 10:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Annualised returns in python. Urgent request shivamdang 2 2,197 Apr-12-2020, 07:37 AM
Last Post: shivamdang
  Python function returns inconsistent results bluethundr 4 3,240 Dec-21-2019, 02:11 AM
Last Post: stullis
  why the generator expression after IF always returns True HenryJ 1 2,739 Feb-13-2018, 07:14 AM
Last Post: buran
  receive from a generator, send to a generator Skaperen 9 5,565 Feb-05-2018, 06:26 AM
Last Post: Skaperen
  Python Code Generator KatherineHov 11 8,937 Jul-27-2017, 10:04 AM
Last Post: MTVDNA
  Python object that returns tuples? alfredocabrera 1 2,908 Mar-27-2017, 12:27 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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