0

Being new to Python, but still unescapable, I'm stuck on this problem. What I'm trying to do is pass a string into the function to get it extended.

Here is what I have:

def replace(source, destination):
    local_source = src_tree.find("source")
    local_destination = dest_tree.find("destination")
    local_destination.extend(local_source)

replace(source=".//animal-list/dog", destination=".//animal-list/dog")

This piece of code will work if I don't place it in a function. But because I have hundreds of these "expend" that I have to achieve, why not the good o' function calling.

Originally I have this, and it works as what I need:

src = src_tree.find('.//animal-list/dog')
dest = dest_tree.find('.//animal-list/dog')
dest.extend(src)

And what that would do is "replace" the dest dog with src dog. Works perfect, but I'm trying to make it into a function for easier use.

My question would be, what am I doing wrong in the function? Since it is tossing up a exception.

Traceback (most recent call last):
  File "test.py", line 28, in <module>
    replace(source=".//animal-list/dog", destination=".//animal-list/dog")
  File "test.py", line 13, in replace
    local_destination.extend(local_source)
AttributeError: 'NoneType' object has no attribute 'extend'
4
  • 2
    "source" and "destination" shouldn’t be in quotes if you want to pass their values. Commented Jan 17, 2014 at 3:36
  • Strange how I thought I tried that and still gave me exception. I might be wrong, been going at this program (not particularly this problem) for over 12 hours now. (Rushing for Wednesday release). Problem is, I speak no Python before Wednesday. Commented Jan 17, 2014 at 3:46
  • @tyler Are you sure it's the same exception? Commented Jan 17, 2014 at 4:12
  • It might have been a different exception. I think I just been in front of the computer for far too long. Commented Jan 17, 2014 at 4:48

2 Answers 2

2

You've quoted things that should be variables (source and destination). It should be:

def replace(source, destination):
    local_source = src_tree.find(source)
    local_destination = dest_tree.find(destination)
    local_destination.extend(local_source)
Sign up to request clarification or add additional context in comments.

Comments

1

Here you are passing a literal string, instead of the variable

local_destination = dest_tree.find("destination")

Perhaps dest_tree.find is returning None because of that. Try this instead

local_destination = dest_tree.find(destination)

And likewise where you have used "source" instead of source

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.