Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52291

Re: beginner question (True False help)

References <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> <35939249-9d6f-46ac-b0b6-6ab8152cdae9@googlegroups.com>
From Joshua Landau <joshua@landau.ws>
Date 2013-08-10 02:22 +0100
Subject Re: beginner question (True False help)
Newsgroups comp.lang.python
Message-ID <mailman.415.1376097771.1251.python-list@python.org> (permalink)

Show all headers | View raw


On 10 August 2013 00:34,  <eschneider92@comcast.net> wrote:
> What does global mean?

Python has "scopes" for its variables. Most programming languages do.
A "scope" is a restriction on where variables exist -- they exist only
within the scope.

This can be seen in this example:

    def function():
        # A new "scope" is made when you enter a function
        variable = 100

    function()
    print(variable)
    # Error, as variable doesn't exist outside of "function"'s scope

There are lots of different "scopes" in code. Every function has one,
and there are some more too.

One of the scopes is the "global" scope. This is the scope *outside*
of all the functions and other scopes. Everything in the file is
within this sope:

    # Make in global scope
    variable = 100

    def function():
       # Works because we're inside the global scope
        print(variable)

    # Prints "100"
    function()

So "a = b" inside the function applies to the function's scope, but
when accessing variables (such as "print(variable)") it will look in
all of the outer scopes too.

If you want to write "a = b" inside the function and change the global
scope, you need to say that "a" refers to the "a" in the global scope.
You do that like this:

    def function():
        # "variable" is in the global scope, not the functions'
        global variable
        variable = 100

    function()
    # Prints "100"
    print(variable)


Does that help you understand what "global" means?

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

beginner question (True False help) eschneider92@comcast.net - 2013-08-07 01:17 -0700
  Re: beginner question (True False help) Joshua Landau <joshua@landau.ws> - 2013-08-07 09:42 +0100
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-07 13:59 -0700
    Re: beginner question (True False help) Dave Angel <davea@davea.name> - 2013-08-08 01:18 +0000
  Re: beginner question (True False help) Larry Hudson <orgnut@yahoo.com> - 2013-08-07 19:49 -0700
  Re: beginner question (True False help) wxjmfauth@gmail.com - 2013-08-07 23:20 -0700
    Re: beginner question (True False help) Chris Angelico <rosuav@gmail.com> - 2013-08-08 12:41 +0100
    Re: beginner question (True False help) Terry Reedy <tjreedy@udel.edu> - 2013-08-08 16:29 -0400
      Re: beginner question (True False help) wxjmfauth@gmail.com - 2013-08-09 01:05 -0700
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 15:27 -0700
    Re: beginner question (True False help) Joshua Landau <joshua@landau.ws> - 2013-08-10 00:05 +0100
    Re: beginner question (True False help) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-08-09 22:58 -0400
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 15:28 -0700
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 16:14 -0700
    Re: beginner question (True False help) Joshua Landau <joshua@landau.ws> - 2013-08-10 00:30 +0100
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 16:24 -0700
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 16:34 -0700
    Re: beginner question (True False help) Joshua Landau <joshua@landau.ws> - 2013-08-10 02:22 +0100
    Re: beginner question (True False help) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-10 01:40 +0000
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 16:40 -0700
    Re: beginner question (True False help) MRAB <python@mrabarnett.plus.com> - 2013-08-10 01:39 +0100
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 16:43 -0700
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 18:08 -0700

csiph-web