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.
Here's how I would do it:
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)
PEP 8 has guidelines on how things should be named in python CamelCase is usually reserved for class names
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.
If you don't know about `if name == 'main': you should read up on it.
Don't forget to use meaningful variable names and not override the builtins (i.e. file = "foo.txt" would override the file builtin)
You can learn more about context managers here
An edit using
globals()
, FOR WHICH NO USE CASE EXISTS AT ALL.