2 个函数对新函数的 2 个响应

发布于 2024-12-27 17:51:46 字数 966 浏览 1 评论 0原文

我当前正在编写一小段代码,用于将保存的文档中的 Web 服务器页面的 etag 与服务器上的 etag 进行比较。如果它们不同,代码将指出这一点。我的代码如下: -

import httplib

def currentTag():
    f = open('C:/Users/ME/Desktop/document.txt')
    e = f.readline()
    newTag(e)

def newTag(old_etag):
    c = httplib.HTTPConnection('standards.ieee.org')
    c.request('HEAD', '/develop/regauth/oui/oui.txt')
    r = c.getresponse()
    current_etag = r.getheader('etag').replace('"', '')
    compareTag(old_etag, current_etag)

def compareTag(old_etag, current_etag):
    if old_etag == current_etag:
        print "the same"
    else:
        print "different"

if __name__ == '__main__':
    currentTag()

现在,检查我的代码,实际上没有理由将“etag”从 currentTag() 方法传递到 newTag() 方法,因为newTag() 中不会处理预先存在的 etag。尽管如此,如果我不这样做,我如何将两个不同的值传递给 compareTag()。例如,在定义 compareTag() 时,如何从 currentTag() 方法传递“etag”以及从 newTag()< 传递“current_etag” /代码>方法?

I'm current writing a short bit of code that will compare an etag for a web server page in a saved document to the etag on the server. If they are different, the code will indicate this. My code is below:-

import httplib

def currentTag():
    f = open('C:/Users/ME/Desktop/document.txt')
    e = f.readline()
    newTag(e)

def newTag(old_etag):
    c = httplib.HTTPConnection('standards.ieee.org')
    c.request('HEAD', '/develop/regauth/oui/oui.txt')
    r = c.getresponse()
    current_etag = r.getheader('etag').replace('"', '')
    compareTag(old_etag, current_etag)

def compareTag(old_etag, current_etag):
    if old_etag == current_etag:
        print "the same"
    else:
        print "different"

if __name__ == '__main__':
    currentTag()

Now, reviewing my code, there is actually no reason to pass 'etag' from the currentTag() method to the newTag() method given that the pre-existing etag is not processed in newTag(). Nonetheless, if I don't do this, how can I pass two different values to compareTag(). So for example, when defining compareTag(), how can I pass 'etag' from the currentTag() method and 'current_etag' from the newTag() method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟花肆意 2025-01-03 17:51:46

你不应该像这样链接你的函数调用,有一个连续调用函数的主要代码块,如下所示:

if __name__ == '__main__':
  currtag = currentTag()
  newtag = newTag()
  compareTag(currtag,newtag)

调整你的函数以返回相关数据

函数的基本思想是它返回数据,你通常使用函数进行一些处理并返回一个值,而不是用于控制流。

you shouldn't chain your function calls like that, have a main block of code that calls the functions serially, like so:

if __name__ == '__main__':
  currtag = currentTag()
  newtag = newTag()
  compareTag(currtag,newtag)

adjust your functions to return the relevant data

the basic idea of a function is that it returns data, you usually use functions to do some processing and return a value and not for control flow.

御守 2025-01-03 17:51:46

将 main 更改为:

if __name__ == '__main__':
    compareTag(currentTag(), newTag())

然后让 currentTag() 返回 e 和 newTag() 返回 current_etag

change your main to:

if __name__ == '__main__':
    compareTag(currentTag(), newTag())

and then have currentTag() return e and newTag() return current_etag

葬﹪忆之殇 2025-01-03 17:51:46
def checkTags():
    c = httplib.HTTPConnection('standards.ieee.org')
    c.request('HEAD', '/develop/regauth/oui/oui.txt')
    r = c.getresponse()
    with open('C:/Users/ME/Desktop/document.txt', 'r') as f:
        if f.readline() == r.getheader('etag').replace('"', ''): print "the same"
        else: print "different"
def checkTags():
    c = httplib.HTTPConnection('standards.ieee.org')
    c.request('HEAD', '/develop/regauth/oui/oui.txt')
    r = c.getresponse()
    with open('C:/Users/ME/Desktop/document.txt', 'r') as f:
        if f.readline() == r.getheader('etag').replace('"', ''): print "the same"
        else: print "different"
软甜啾 2025-01-03 17:51:46

您可以将变量(即 etag)设置为全局变量

you could make the variables (i.e. etag) global

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文