简体   繁体   English

在另一个python程序中使用函数中的变量

[英]Using variables from a function in another python program

So I'm trying to write a python program to integrate into a greater in-house developed python application. 因此,我试图编写一个python程序以集成到一个更大的内部开发的python应用程序中。 This program I'm writing needs to generate an xml document and populate the fields with data stored in variables from another function in a different module. 我正在编写的该程序需要生成一个xml文档,并使用存储在不同模块中另一个函数的变量中的数据填充字段。

After realizing I can't have both programs import each other (main program needs to call xmlgen.py to generate the xml doc, while xmlgen.py needs to utilize variables in the main program to generate that doc), I'm a little bit at a loss as to what to do here. 意识到之后,我无法让两个程序互相导入(主程序需要调用xmlgen.py来生成xml文档,而xmlgen.py需要利用主程序中的变量来生成该doc),我有点有点无所适从。

In the example shown below, xmlgen.py needs to use variables from the function sendFax in Faxer.py. 在下面显示的示例中,xmlgen.py需要使用来自Faxer.py中的sendFax函数的变量。 Faxer.py needs to call xmlgen.py to generate the document. Faxer.py需要调用xmlgen.py来生成文档。

snippet from xmlgen.py: xmlgen.py的片段:

from lxml import etree
from Faxer import coverPage, ourOrg, ourPhonenum, ourFaxnum, emailAddr, sendReceipt, webAddr, comments
from Faxer import sendFax

def generateXml():
    #xml file structure
    root = etree.Element('schedule_fax')
...
~ A bunch of irrelevant xml stuff
...

    grandchild_recipient_name = etree.Element('name')
    grandchild_recipient_name.text = cliName
    child_recipient.append(grandchild_recipient_name)

Now the piece of the main program I need to utilize the "cliName" variable from... 现在,我需要在主程序中利用...中的“ cliName”变量

def sendFax(destOrg, destFax, cliName, casenum, attachments, errEAddr, comment, destName):
    creds=requests.auth.HTTPBasicAuth(user,password)

    allData=''
    allData+='<schedule_fax>\n'

    allData+='<cover_page>\n'
    allData+='<url>'+prepXMLString(coverPage)+'</url>\n'
    allData+='<enabled>true</enabled>\n'
    allData+='<subject>'+prepXMLString(cliName)+' - case # '+str(casenum)+'</subject>\n'

Now when I try to import sendFax function from Faxer.py, I'm unable to call any of the variables from the function like, 现在,当我尝试从Faxer.py导入sendFax函数时,无法从函数中调用任何变量,例如,

grandchild_recipient_name.text = sendFax.cliName

does not work. 不起作用。 What am i doing wrong here?? 我在这里做错了什么? I'm not a python guru and am in fact quite new to all of this, so I'm hoping it's something simple. 我不是python专家,实际上对这一切都是新手,所以我希望它很简单。 Should I just dump everything into a new function in the main program? 我是否应该将所有内容都转储到主程序的新函数中?

As pointed out above, you are trying to reference cliName as if it is an attribute of the function. 如上所述,您正在尝试引用cliName,就好像它是函数的属性一样。 This would be closer to being correct if sendFax was a class, but that's another subject. 如果sendFax是一个类,这将更接近正确,但这是另一个主题。 The snippet you have provided is simply a function definition. 您提供的代码片段只是一个函数定义。 It doesn't guarantee that this function is ever actually used or give you any idea what cliName actually is, cliName is just the name used by the function internallt to describe the 3rd value supplied as input. 它不能保证实际上曾经使用过此函数,也不保证您真正知道cliName是什么,cliName只是函数internallt用来描述作为输入提供的第三个值的名称。

What you need to do is find where sendFax is actually used, rather than where it is defined. 您需要做的是找到sendFax的实际使用位置,而不是定义位置。 Then look at what the variables are called which are passed into it. 然后查看传递给它的变量称为什么。 There are two ways to pass variables into a function: by position and by name. 有两种方法可以将变量传递到函数中:按位置和按名称。 If the variables are being passed by pposition you will find something like: 如果变量是通过pposition传递的,则会发现类似以下内容:

sendFax(some_name,some_other_name,yet_another_name,...

The third one of these will be the variable which becomes cliName inside the function. 其中的第三个将是在函数内部变为cliName的变量。

If being passed by name you will see something like 如果通过名称传递,您会看到类似

sendFax(cliName=yet_another_name,...

Where once again yet_another_name is the thing which becomes cliName. 这里的yet_another_name再次成为cliName。

Depending on how the programme is structured you may be able to refer to yet_another_name from your program and get the value you need. 根据程序的结构,您也许可以从程序中引用yet_another_name并获取所需的值。

from Faxer import yet_another_name

But this will only work if Faxer runs and finishes with the one and only value of yet_another_name assigned. 但这仅在Faxer运行并以指定的yet_another_name一个唯一值结束时才起作用。 If Faxer iterates through lots of values of yet_another_name, or simply doesn't run sensibly when called as an import you'll need a more sophisticated approach. 如果Faxer遍历yet_another_name的许多值,或者在作为导入被调用时根本不明智地运行,则需要一种更复杂的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM