对Python不熟悉,但仍然无法避免,我坚持这个问题。我要做的是将一个字符串传递给函数以使其extend
编辑。
以下是我的内容:
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")
如果我不将它放在函数中,这段代码就可以工作。但是因为我必须实现数百个这样的“expend
”,为什么不是好的o'函数调用。
最初我有这个,它可以满足我的需要:
src = src_tree.find('.//animal-list/dog')
dest = dest_tree.find('.//animal-list/dog')
dest.extend(src)
那样做的是用dest
狗“替换”src
狗。效果很好,但我正在尝试将它变成一个更容易使用的功能。
我的问题是,我在功能上做错了什么?因为它正在抛出异常。
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'
答案 0 :(得分:2)
您引用了应该是变量的内容(source
和destination
)。
它应该是:
def replace(source, destination):
local_source = src_tree.find(source)
local_destination = dest_tree.find(destination)
local_destination.extend(local_source)
答案 1 :(得分:1)
这里传递一个文字字符串,而不是变量
local_destination = dest_tree.find("destination")
或许dest_tree.find
因此返回None。试试这个
local_destination = dest_tree.find(destination)
同样,您使用"source"
代替source