python - Need help understanding how this recursive function is working -


Here is a function (credit to user abot, to provide it in another question)

  Diff Transgress (FTP): level = {} entry (for path in path) in ftp.nlst () if the path is not ('.', '..')): ftp.cwd (entry) Level [entry] = reverse level  

This is what I do not understand: When the python enters the function, it creates an empty dictionary ( level ). For loop, it stores a directory name in the dictionary as a key, for the value of that key, the dragon enters the function again and searches for a directory and it becomes the value of that key .

But, how do you remember the values ​​within the levels? I mean, it should not be reset / empty every time Pygm enters the function?

Each "instance" of the function has its own copy of level and There is no side effect between different codes of level .

Take this folder tree:

  root `-home | - lyrae | | - ftp.py | When you call  ftp  to  Route  then : 

:

<<>
  • FTP (root) creates a blank level
  • ftp ( Root) Calculates the subfolders (home) .
  • FTP (root) first selects the subfolder and changes the directory in it.
  • set level [home] for the result of ftp in the current folder.

    • FTP (Home) creates an empty level dictionary
    • Ftp (home) subfolders enumerates:
    • FTP (Home) first selects the subfolder and changes the directory in it.
    • ftp
          < FTP (Lira) creates a blank level dictionary
        • FP (Lira) calculates subfolders: () .
        • ftp (lyrae) is out of the subfolder for parsing and return level .
        • ftp (home) completes the assignment: level = {'lyrae': {}}
        • FTP (home) changes to the next folder.
        • FTP (home) set level [badp] for the result of ftp in the current code.

        • ftp (<< Code> level
        • ftp (badp) calculates the subfolder : () . / li>
        • FTP (badp) is out of the subfolder for the pars and return level .
        • > completes the FTP (home) assignment: level = {'lirai': {}, 'badp': { }}
        • level
        • FTP is out of the subfolder for parsing and returning assigns Nt: level = {'home': {'lyrae': {}, 'badp ': {}}}
        • ftp (root) level is out of the subfolder for parsing and returning.

    Comments

    Popular posts from this blog

    c# - How to capture HTTP packet with SharpPcap -

    php - Multiple Select with Explode: only returns the word "Array" -

    php - jQuery AJAX Post not working -