0

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

Óscar López
  • 232,561
  • 37
  • 312
  • 386
user2070615
  • 53
  • 2
  • 8
  • 1
    You should be able to access it. Show us the code where you try to access it. – Ned Batchelder Mar 21 '13 at 16:14
  • htmlfrom("http://stackoverflow.com/questions/15552601/creating-global-variable-in-python-3-from-functions") What will this data be saved as: html_stackoverflow >>> print(html_stackoverflow) Traceback (most recent call last): File "", line 1, in print(html_stackoverflow) NameError: name 'html_stackoverflow' is not defined – user2070615 Mar 21 '13 at 16:17
  • You can see I attempted to print(thevariablename) and I got the error: Traceback (most recent call last): File "", line 1, in print(html_stackoverflow) NameError: name 'html_stackoverflow' is not defined – user2070615 Mar 21 '13 at 16:19
  • 1
    Hmm, can you make one paste in the original post, with the same variable names? You're switching the names, so it's not clear what's going on. Unless the problem is that the function creates `variable_for_raw_data`, and you are trying to access `html_stackoverflow`. – Ned Batchelder Mar 21 '13 at 16:30
  • Yes. I feel very stupid. I was trying to access the wrong variable!!! – user2070615 Mar 21 '13 at 16:36

1 Answers1

1

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()
Community
  • 1
  • 1
John
  • 13,197
  • 7
  • 51
  • 101
  • How do I make it so that whatever I type in: input("What will this data be saved as: ") becomes the variable? – user2070615 Mar 21 '13 at 18:18
  • 1
    It's generally considered bad to do this, so don't =). But `globals()["my_shiny_variable"] = "a_shiny_variable"` then `print my_shiny_variable`. – John Mar 21 '13 at 18:25
  • could you post the code how it would look. I think you mean: 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()["my_shiny_variable"] = "a_shiny_variable" my_shiny_variable = response.read() – user2070615 Mar 21 '13 at 18:54
  • You are very helpful by the way. – user2070615 Mar 21 '13 at 18:55
  • can anyone help me with this? I need for the variable that I enter in the the input prompt to trigger the HTML rather than the "x" variable. – user2070615 Mar 21 '13 at 20:30
  • for example, when I write: def html(website_url): import urllib.request response = urllib.request.urlopen(website_url) x = input("Enter variable: ") x = response.read() When I type in whatever variable I type in the input after the function it just gives me an error. How do I make what I type become the variable? – user2070615 Mar 22 '13 at 15:32