Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Can someone help me understand how this recursive function works?

recursive

  • Please log in to reply
4 replies to this topic

#1 demo53

demo53

    CC Newcomer

  • Member
  • PipPip
  • 24 posts

Posted 15 July 2011 - 08:33 PM

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')
  • 0

#2 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 15 July 2011 - 11:16 PM

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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 demo53

demo53

    CC Newcomer

  • Member
  • PipPip
  • 24 posts

Posted 16 July 2011 - 05:46 AM

Ohh I understand now.

Thanks for the help.
  • 0

#4 demo53

demo53

    CC Newcomer

  • Member
  • PipPip
  • 24 posts

Posted 16 July 2011 - 06:44 AM

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')?
  • 0

#5 demo53

demo53

    CC Newcomer

  • Member
  • PipPip
  • 24 posts

Posted 16 July 2011 - 06:56 AM

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
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download