Python Forum
Having hard time understanding the function self-returning itself twice
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having hard time understanding the function self-returning itself twice
#1
I am trying to understand the program written here for Find all possible unique subsets from a set of distinct integers

https://www.w3resource.com/python-exerci...cise-4.php

here in the code i am not sure what
return  self.subsetsRecur(current, sset[1:]) +self.subsetsRecur(current + [sset[0]], sset[1:])
it does, when i see the output i tend to think that python first execute the all calls of
self.subsetsRecur(current, sset[1:])
then execute
 self.subsetsRecur(current + [sset[0]], sset[1:])
and concatenating the results, but i think i may be wrong, because i dont know where this results can be stored ? we are not using much of variables to store, when i see the output values for each calls it is bit confusing, can anyone please confirm or explain this part. Thank you.

class py_solution:
    def sub_sets(self, sset):
       
        return self.subsetsRecur([], sorted(sset))
    
    def subsetsRecur(self, current, sset):
        print('before if ',current,sset) 
        if sset:
            print(' if ',current,sset) 
            return  self.subsetsRecur(current, sset[1:]) +self.subsetsRecur(current + [sset[0]], sset[1:])
        print('After if ', current,sset) 
        return [current]
    
print(py_solution().sub_sets([4,5,6]))
The results with prints :

before if  [] [4, 5, 6]
 if  [] [4, 5, 6]
before if  [] [5, 6]
 if  [] [5, 6]
before if  [] [6]
 if  [] [6]
before if  [] []
After if  [] []
before if  [6] []
After if  [6] []
before if  [5] [6]
 if  [5] [6]
before if  [5] []
After if  [5] []
before if  [5, 6] []
After if  [5, 6] []
before if  [4] [5, 6]
 if  [4] [5, 6]
before if  [4] [6]
 if  [4] [6]
before if  [4] []
After if  [4] []
before if  [4, 6] []
After if  [4, 6] []
before if  [4, 5] [6]
 if  [4, 5] [6]
before if  [4, 5] []
After if  [4, 5] []
before if  [4, 5, 6] []
After if  [4, 5, 6] []
[[], [6], [5], [5, 6], [4], [4, 6], [4, 5], [4, 5, 6]]
Reply
#2
One could have better named the parameters as
def subsetsRecur(self, chosen, candidates):
    ...
This function returns the list of all subsets containing all the chosen items plus some of the candidates. This list will be the sum of two lists: the list of all subsets containing all the chosen items plus some of the candidates but the first candidate, and the list of all subsets containing all the chosen items and the first candidate plus some of the remaining candidates.

That is the meaning of this sum of two lists. Of course there is a special case when the list of candidates is empty. In this case, there is only one subset, the list of chosen items, and the function returns a list containing this single subset.
Reply
#3
Variables in Python are a reference to an object, not the storage for an object. You can write Python programs that don't use any variables at all.
print(1 + 2)
This is a valid program. It has a Python object that is the integer 1, and another that is the integer 2. It adds 1 and 2 together and we get another Python object for the result. There is no variable referencing the result, but there is still an object. When print is called it converts the Python object 3 to a string, yet another Python object, appends a linefeed, creating yet another Python object, and prints the string to stdout.

Your recursive function calls do not need variables. Just we were only interested in the result of 1 + 2 your function is only interested in the result of self.subsetsRecur(current, sset[1:]) +self.subsetsRecur(current + [sset[0]], sset[1:])

Are you wondering what happens to all those Python objects that are created? A vast majority of them get tossed in the trash when no longer needed, their storage made available to be reused for new Python objects. The only objects that are not tossed in the trash are those referenced by a variable.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hard time trying to figure out the difference between two strings carecavoador 2 675 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  return next item each time a function is executed User3000 19 2,276 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  Understanding a function ebolisa 3 777 Jul-14-2023, 06:03 PM
Last Post: snippsat
  Understanding venv; How do I ensure my python script uses the environment every time? Calab 1 2,247 May-10-2023, 02:13 PM
Last Post: Calab
  Why my function is returning None? PauloDAS 6 1,754 Jul-17-2022, 11:17 PM
Last Post: Skaperen
  time function does not work tester_V 4 3,011 Oct-17-2021, 05:48 PM
Last Post: tester_V
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,216 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  Pausing and returning to function? wallgraffiti 1 2,154 Apr-29-2021, 05:30 PM
Last Post: bowlofred
  Can you end the Time.sleep function boier96 9 9,416 Jan-16-2021, 10:09 PM
Last Post: Serafim
  Why is the function returning None for a * b instead of number? omm 10 4,283 Nov-05-2020, 01:17 PM
Last Post: omm

Forum Jump:

User Panel Messages

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