Python Forum
Recursion, with "some_dict={}" function parameter.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Recursion, with "some_dict={}" function parameter.
#1
I'm not getting some python(3.5.2) behaviour.

I got a function def get_links_titles( links_list, titles_dict={}, r=0 ):. (Key-part: titles_dict={})

In it I recall(recursion) it with titles_del = get_links_titles( case_dict['redirlinks'], {}, r=r+1 ) it works as intended. The recursive case start with an empty titles_dict case.

But when I don't use the , {} part, and use titles_del = get_links_titles( case_dict['redirlinks'], r=r+1 )
The titles_dict in the recursion case is not empty, but contains the data titles_dict had before the recursive call. (???).

Ergo: What Python behaviour rule(s) I'm I missing to make sense of that last case.

---

Mmm, seems that titles_dict={} as function parameter is not a good idea based on some similar down the line unsuspected hiccup.
So ditched it in the def-part and moved it inside the function. (not needed in def + wip function. re-investigate later.)
Reply
#2
Dictionaries are objects that are always passed as links to the original. Never do the following:
def get_links_titles( links_list, titles_dict={}, r=0 ):
     pass
in this case, you will have only one object titles_dict through all of the function calls. Do like this:

def get_links_titles(links_list, titles_dict=None, r=0):
     titles_dict = titles_dict or {}
Reply
#3
The same is with lists as default arguments
https://nikos7am.com/posts/mutable-default-arguments/
Reply
#4
Aha. Its starting to make more sense to me now.

Definitely a subject I need to explore some more (todo). Additional Link definitely going to help with that.
(I think I used this ones to my benefit, but without actually realising why or how it worked)

Thanks for putting me on the right track. :-)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  determine parameter type in definition function akbarza 1 550 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 892 Aug-07-2023, 05:58 PM
Last Post: Karp
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,656 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,009 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  use NULL as function parameter which is a pointer function? oyster 0 2,466 Jul-11-2020, 09:02 AM
Last Post: oyster
  Lambda function recursion error DeadlySocks 1 2,010 Apr-13-2020, 05:09 PM
Last Post: deanhystad
  what would you call the input for the parameter(s) of a function you have defined? rix 3 2,401 Dec-16-2019, 12:04 AM
Last Post: rix
  def function default parameter expressions Skaperen 2 2,500 Aug-22-2019, 10:58 PM
Last Post: Skaperen
  recursion function (python beginner) everafter 3 2,839 Aug-19-2019, 07:24 AM
Last Post: buran
  Input as function parameter dan789 1 2,045 Mar-08-2019, 05:19 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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