2 个函数对新函数的 2 个响应
我当前正在编写一小段代码,用于将保存的文档中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不应该像这样链接你的函数调用,有一个连续调用函数的主要代码块,如下所示:
调整你的函数以返回相关数据
函数的基本思想是它返回数据,你通常使用函数进行一些处理并返回一个值,而不是用于控制流。
you shouldn't chain your function calls like that, have a main block of code that calls the functions serially, like so:
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.
将 main 更改为:
然后让
currentTag()
返回 e 和newTag()
返回 current_etagchange your main to:
and then have
currentTag()
return e andnewTag()
return current_etag您可以将变量(即 etag)设置为全局变量
you could make the variables (i.e. etag) global