Frage

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'
War es hilfreich?

Lösung

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)

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top