[英]Creating global variable in python 3 from functions
I was wondering why I can't access the variable: "variable_for_raw_data" after the function ends. 我想知道为什么函数结束后不能访问变量:“ variable_for_raw_data”。 The code is like this:
代码是这样的:
def htmlfrom(Website_URL):
import urllib.request
response = urllib.request.urlopen(Website_URL)
variable_for_raw_data =(input("What will this data be saved as: "))
global variable_for_raw_data
variable_for_raw_data = response.read()
Now why can't I access the variable "variable_for_raw_data" after the functions ends? 现在,为什么函数结束后我不能访问变量“ variable_for_raw_data”?
Things to note: 注意事项:
Python 3.3 urllib NOT urllib2 Python 3.3 urllib不是urllib2
It looks like you're trying to dynamically create variables, I would imagine that your code looks something like this. 看起来您正在尝试动态创建变量,我想您的代码看起来像这样。
def htmlfrom(website_url):
import urllib.request
response = urllib.request.urlopen(website_url)
variable_for_raw_data =(input("What will this data be saved as: "))
global variable_for_raw_data
variable_for_raw_data = response.read()
if __name__ == "__main__":
htmlfrom("www.stackoverflow.com")
#html_stackoverflow is never created it is the value
#of variable_for_raw_data before variable_for_raw_data
#is overridden by response.read()
#entering information into input doesn't create a variable
print(html_stackoverflow)
Here's how I would do it: 这是我的处理方式:
import urllib.request
def htmlfrom(website_url):
'''
docstrings
'''
response = urllib.request.urlopen(website_url)
variable_for_raw_data = response.read()
return variable_for_raw_data
if __name__ == "__main__":
file_name = input("What will this data be saved as: ")
html_from_website = htmlfrom("www.stackoverflow.com")
with open(file_name, 'w') as f:
f.write(html_from_website)
Explanation 说明
if you have your import statement inside the function it is only accessable inside the function (ie other functions can't access it) 如果在函数内部有import语句,则只能在函数内部访问(即其他函数无法访问它)
import urllib.request
PEP 8 has guidelines on how things should be named in python CamelCase is usually reserved for class names PEP 8有关于如何在python中命名的指南CamelCase通常为类名保留
def htmlfrom(website_url):
'''
docstring
'''
Docstrings are usually a good idea. 文档字符串通常是一个好主意。
Check out this question for more information on the proper use of globals . 查看此问题以获取有关正确使用全局变量的更多信息。 Based on what I know about your situation I don't think you need to use them.
根据我对您所处情况的了解,我认为您不需要使用它们。
response = urllib.request.urlopen(website_url)
variable_for_raw_data = response.read()
return variable_for_raw_data
If you don't know about `if name == ' main ': you should read up on it. 如果您不知道`if name ==' main ':您应该继续阅读。
if __name__ == "__main__":
Don't forget to use meaningful variable names and not override the builtins (ie file = "foo.txt" would override the file builtin) 不要忘记使用有意义的变量名而不覆盖内置函数(即file =“ foo.txt”将覆盖内置文件)
file_name = input("What will this data be saved as: ")
html_from_website = htmlfrom("www.stackoverflow.com")
You can learn more about context managers here 您可以在此处了解有关上下文管理器的更多信息
with open(file_name, 'w') as f:
f.write(html_from_website)
An edit using globals()
, FOR WHICH NO USE CASE EXISTS AT ALL . 使用
globals()
进行编辑,因为根本不存在用例 。
def htmlfrom(website_url):
import urllib.request
response = urllib.request.urlopen(website_url)
variable_for_raw_data =(input("What will this data be saved as: "))
globals()[variable_for_raw_data] = response.read()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.