如何让函数在被调用时相互感知,例如使用 ElementTree 包时
How to make the functions be aware of each other when they are called, e.g. when using the ElementTree package
如果标题问题表述不清楚,下面的代码会更好的解释。此代码工作正常:
#!/usr/bin/python3
import sys
import os
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import *
country_arg = 'usa'
transport_name_arg = 'train'
route_data = "some route"
world = ET.Element('world')
country = ET.SubElement(world, 'country')
country.text = country_arg
event = ET.SubElement(world, 'event')
transport = ET.SubElement(event, 'transport')
transport.set('name',transport_name_arg)
route = ET.SubElement(event, 'route')
comment = Comment(route_data)
route.append(comment)
c = ET.SubElement(event, 'c')
tree = ET.ElementTree(world)
tree.write("filename.xml")
但是我需要创建和使用在实际脚本的不同位置调用它们的函数。我认为当函数运行时它与它所代表的代码块运行时相同。然而, function_1 的结果似乎没有保存在内存中?
这是与上面相同的脚本,但代码被分成两个函数并且它不起作用,就好像一旦 function_2 被调用,function_1 的结果就丢失了:
#!/usr/bin/python3
import sys
import os
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import *
country_arg = 'usa'
transport_name_arg = 'train'
route_data = "some route"
def function_1():
world = ET.Element('world')
country = ET.SubElement(world, 'country')
country.text = country_arg
event = ET.SubElement(world, 'event')
transport = ET.SubElement(event, 'transport')
transport.set('name',transport_name_arg)
route = ET.SubElement(event, 'route')
def function_2():
comment = Comment(route_data)
route.append(comment)
c = ET.SubElement(event, 'c')
function_1()
function_2()
tree = ET.ElementTree(world)
tree.write("filename.xml")
错误:
Traceback (most recent call last):
File "test-WITH-functions-xml.py", line 26, in <module>
function_2()
File "test-WITH-functions-xml.py", line 22, in function_2
route.append(comment)
NameError: name 'route' is not defined
这段代码有效,谢谢大家,很好的学习经验:
#!/usr/bin/python3
import sys
import os
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import *
country_arg = 'usa'
transport_name_arg = 'train'
route_data = "some route"
def function_1():
global world
global country
global event
global transport
global route
world = ET.Element('world')
country = ET.SubElement(world, 'country')
country.text = country_arg
event = ET.SubElement(world, 'event')
transport = ET.SubElement(event, 'transport')
transport.set('name',transport_name_arg)
route = ET.SubElement(event, 'route')
def function_2():
global comment
global c
comment = Comment(route_data)
route.append(comment)
c = ET.SubElement(event, 'c')
function_1()
function_2()
tree = ET.ElementTree(world)
tree.write("filename.xml")
如果标题问题表述不清楚,下面的代码会更好的解释。此代码工作正常:
#!/usr/bin/python3
import sys
import os
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import *
country_arg = 'usa'
transport_name_arg = 'train'
route_data = "some route"
world = ET.Element('world')
country = ET.SubElement(world, 'country')
country.text = country_arg
event = ET.SubElement(world, 'event')
transport = ET.SubElement(event, 'transport')
transport.set('name',transport_name_arg)
route = ET.SubElement(event, 'route')
comment = Comment(route_data)
route.append(comment)
c = ET.SubElement(event, 'c')
tree = ET.ElementTree(world)
tree.write("filename.xml")
但是我需要创建和使用在实际脚本的不同位置调用它们的函数。我认为当函数运行时它与它所代表的代码块运行时相同。然而, function_1 的结果似乎没有保存在内存中? 这是与上面相同的脚本,但代码被分成两个函数并且它不起作用,就好像一旦 function_2 被调用,function_1 的结果就丢失了:
#!/usr/bin/python3
import sys
import os
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import *
country_arg = 'usa'
transport_name_arg = 'train'
route_data = "some route"
def function_1():
world = ET.Element('world')
country = ET.SubElement(world, 'country')
country.text = country_arg
event = ET.SubElement(world, 'event')
transport = ET.SubElement(event, 'transport')
transport.set('name',transport_name_arg)
route = ET.SubElement(event, 'route')
def function_2():
comment = Comment(route_data)
route.append(comment)
c = ET.SubElement(event, 'c')
function_1()
function_2()
tree = ET.ElementTree(world)
tree.write("filename.xml")
错误:
Traceback (most recent call last):
File "test-WITH-functions-xml.py", line 26, in <module>
function_2()
File "test-WITH-functions-xml.py", line 22, in function_2
route.append(comment)
NameError: name 'route' is not defined
这段代码有效,谢谢大家,很好的学习经验:
#!/usr/bin/python3
import sys
import os
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import *
country_arg = 'usa'
transport_name_arg = 'train'
route_data = "some route"
def function_1():
global world
global country
global event
global transport
global route
world = ET.Element('world')
country = ET.SubElement(world, 'country')
country.text = country_arg
event = ET.SubElement(world, 'event')
transport = ET.SubElement(event, 'transport')
transport.set('name',transport_name_arg)
route = ET.SubElement(event, 'route')
def function_2():
global comment
global c
comment = Comment(route_data)
route.append(comment)
c = ET.SubElement(event, 'c')
function_1()
function_2()
tree = ET.ElementTree(world)
tree.write("filename.xml")