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
#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


Messages In This Thread
RE: Having hard time understanding the function self-returning itself twice - by deanhystad - Aug-15-2020, 08:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Hard time trying to figure out the difference between two strings carecavoador 2 694 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  return next item each time a function is executed User3000 19 2,353 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  Understanding a function ebolisa 3 804 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,326 May-10-2023, 02:13 PM
Last Post: Calab
  Why my function is returning None? PauloDAS 6 1,793 Jul-17-2022, 11:17 PM
Last Post: Skaperen
  time function does not work tester_V 4 3,079 Oct-17-2021, 05:48 PM
Last Post: tester_V
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,296 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  Pausing and returning to function? wallgraffiti 1 2,181 Apr-29-2021, 05:30 PM
Last Post: bowlofred
  Can you end the Time.sleep function boier96 9 9,561 Jan-16-2021, 10:09 PM
Last Post: Serafim
  Why is the function returning None for a * b instead of number? omm 10 4,345 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