在函数中创建python 3中的全局变量 [英] Creating global variable in python 3 from functions
问题描述
我想知道为什么我不能在函数结束后访问变量:variable_for_raw_data。代码如下:
def htmlfrom(Website_URL):
导入urllib.request
response = urllib.request.urlopen(Website_URL)
variable_for_raw_data =(input(这个数据将被保存为:))
全局变量_for_raw_data
variable_for_raw_data = response.read()
现在为什么我不能在函数结束后访问变量variable_for_raw_data?
注意事项:
Python 3.3
urllib NOT urllib2
它看起来像你试图动态创建变量,我想你的代码看起来像这样。
def htmlfrom(website_url):
import urllib.request
response = urllib.request.urlopen(website_url)
variable_for_raw_data =(input(这个数据是什么保存为:))
全局variable_for_raw_data
variable_for_raw_data = r esponse.read()
if __name__ ==__main__:
htmlfrom(www.stackoverflow.com)
#html_stackoverflow永远不会被创建它的值是
#of variable_for_raw_data before variable_for_raw_data
#被response.read()重写
#输入信息不会创建一个变量
print(html_stackoverflow)
以下是我要做的事情:
导入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(将这些数据保存为:)
html_from_website = htmlfrom(www.stackoverflow.com)
打开(file_name,'w')作为f:
f.writ e(html_from_website)
解释
如果你在函数内部有导入语句,它只能在
函数内部访问(例如,其他函数无法访问它)
import urllib.request
a>指导如何在python中命名事物CamelCase通常用于类名
def htmlfrom( website_url):
'''
docstring
'''
Docstrings 通常是一个好主意。
查看此问题以获取有关正确使用全局的更多信息。
根据我所了解的情况,我认为您不需要使用它们。
response = urllib .request.urlopen(website_url)
variable_for_raw_data = response.read()
return variable_for_raw_data
如果您不知道 if name ==' main ':您应该阅读它。
if __name__ ==__main__:
不要忘记使用有意义的变量名称,不要覆盖
builtins (即file =foo.txt会覆盖内置文件)
file_name = input(将这些数据保存为:)
html_from_website = htmlfrom(www.stackoverflow.com)
您可以详细了解上下文管理器 here
<作为f:
f.write(html_from_website)
$带有open(file_name,'w')的code> b $ b
使用 globals()
进行编辑,不存在任何用例存在。
def htmlfrom(website_url):
导入urllib.request
响应= urllib.request.urlopen(website_url)
variable_for_raw_data = (输入(将这些数据保存为:))
globals()[variable_for_raw_data] = response.read()
I was wondering why I can't access the variable: "variable_for_raw_data" after the function ends. 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?
Things to note:
Python 3.3 urllib NOT 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 (i.e. other functions can't access it)
import urllib.request
PEP 8 has guidelines on how things should be named in python CamelCase is usually reserved for class names
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__":
Don't forget to use meaningful variable names and not override the builtins (i.e. file = "foo.txt" would override the file builtin)
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.
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()
这篇关于在函数中创建python 3中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!