Jump to content

Can someone help me understand how this recursive function works?

- - - - -

  • Please log in to reply
4 replies to this topic

#1
demo53

demo53

    Newbie

  • Members
  • PipPip
  • 10 posts
I need help understanding how this recursive function works. I'm having a really hard time trying to grasp the concept.


def split_fully(path):

    parent_path, name = os.path.split(path)

    if name == "":

        return (parent_path, )

    else:

        return split_fully(parent_path) + (name, )


split_fully("/home/mike/Python")



If someone is trying to help me who doesn't program in python, the whole purpose of this function is to split up the directory path '/home/mike/Python' into separate components in a tuple IE:
('/', 'home', 'mike', 'Python') <--- output

os.path.split() will divide your path into two parts- parent directory and the last component in the path. It will put the two components in a tuple
IE: ('/home/mike', 'Python')

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
os.path.split() will split the path into two sections, the head and the tail. The tail contains the last part of the path (say, the Python part of your pathname), the head contains everything else (/home/mike). If name is empty (check the documentation, there are several reasons that happens), then the program returns a single-item tuple containing the parent path (which should just be "/"). The syntax (parent_path, ) does nothing more than package the single element parent_path into a single item tuple. The else clause is using Tuple Concatenation to concatenate the results of another call to split_fully() using the parent path with a single-item tuple containing name. Imagine it working something like this:
split_fully('/home/mike/Python')

split_fully('/home/mike') + ('Python', )

split_fully('/home) + ('mike', ) + ('Python', )

split_fully('/') + ('home', ) + ('mike', ) + ('Python', )

('/', ) + ('home', ) + ('mike', ) + ('Python', )

('/', 'home', 'mike', 'Python')
Hope that explains it... please post if there are any difficulties understanding.
Wow I changed my sig!

#3
demo53

demo53

    Newbie

  • Members
  • PipPip
  • 10 posts
Ohh I understand now.

Thanks for the help.

#4
demo53

demo53

    Newbie

  • Members
  • PipPip
  • 10 posts
Actually, re-looking at it I'm still a bit confused. I don't understand how the new tuple what is being made is being returned. If parent_name stores only '/', and the code 'return (parent_path,)' says to return '/', how is it returning the entire output: ('/', 'home', 'mike', 'Python')?

#5
demo53

demo53

    Newbie

  • Members
  • PipPip
  • 10 posts
Hah, nvm I understand it now. Thanks for all the help!!!! I just couldn't figure it out, because when I was on the function chapter of the book it didn't explain the other ways a function could work. They just showed how a function could return a value. They never showed how it stores local variables of the function from different instances. So now when I'm on the chapter of 'Files and directories', they show a different way of how to use a function...lol




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users