Warning: mysqli::__construct(): (HY000/1203): User howardkn already has more than 'max_user_connections' active connections in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\includes\artfuncs.php on line 21
Failed to connect to MySQL: (1203) User howardkn already has more than 'max_user_connections' active connections
Warning: mysqli::query(): Couldn't fetch mysqli in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\index.php on line 66
Article <mailman.62.1709851700.3452.python-list@python.org>
Deutsch   English   Français   Italiano  
<mailman.62.1709851700.3452.python-list@python.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail
From: Cameron Simpson <cs@cskk.id.au>
Newsgroups: comp.lang.python
Subject: Re: Variable scope inside and outside functions - global statement
 being overridden by assignation unless preceded by reference
Date: Fri, 8 Mar 2024 09:48:08 +1100
Lines: 29
Message-ID: <mailman.62.1709851700.3452.python-list@python.org>
References: <bd2e0672-fc3e-4678-b212-04ab1f45c52c@gmail.com>
 <ZepEKCbZNelFasml@cskk.homeip.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de NpXUvyGtmSuJmvdKFgwZKADAQjZwVGCOnjInx/hBH8fg==
Cancel-Lock: sha1:QZpsGAqU8dfoClnyqhrJtuYH8vM= sha256:6BDr2wrdTtNfJ70hEzBK/mZSNeaIN6m4WXz7ggCXldk=
Return-Path: <cameron@cskk.id.au>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
 dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; 'variable':
 0.05; 'explicitly': 0.07; '>from': 0.09; 'cc:addr:python-list':
 0.09; 'namespace': 0.09; 'page:': 0.09; 'yes.': 0.09; 'cc:no real
 name:2**0': 0.14; 'import': 0.15; 'defined.': 0.16;
 'from:addr:cs': 0.16; 'from:addr:cskk.id.au': 0.16;
 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net':
 0.16; 'received:13.237': 0.16; 'received:13.237.201': 0.16;
 'received:13.237.201.189': 0.16; 'received:cskk.id.au': 0.16;
 'received:id.au': 0.16; 'received:mail.cskk.id.au': 0.16;
 'subject:being': 0.16; 'subject:reference': 0.16; 'url:faq': 0.16;
 'url:programming': 0.16; 'wrote:': 0.16; 'python': 0.16;
 'cc:addr:python.org': 0.20; 'python,': 0.25; 'cc:2**0': 0.25;
 'local': 0.27; 'function': 0.27; 'sense': 0.28; 'header:User-
 Agent:1': 0.30; 'module': 0.31; 'official': 0.32; 'assume': 0.32;
 'unless': 0.32; 'header:In-Reply-To:1': 0.34; 'received:au': 0.35;
 'following': 0.35; 'use': 0.39; 'updates': 0.64; 'received:13':
 0.64; 'received:userid': 0.66; 'outside': 0.67; 'within': 0.69;
 'terms': 0.70; 'global': 0.73; 'global.': 0.84; 'referenced':
 0.84; 'subject: \n ': 0.84; 'body,': 0.91
Mail-Followup-To: Jacob Kruger <jacob.kruger.work@gmail.com>,
 python-list@python.org
Content-Disposition: inline
In-Reply-To: <bd2e0672-fc3e-4678-b212-04ab1f45c52c@gmail.com>
User-Agent: Mutt/2.2.7 (2022-08-07)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
 <python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
 <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
 <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <ZepEKCbZNelFasml@cskk.homeip.net>
X-Mailman-Original-References: <bd2e0672-fc3e-4678-b212-04ab1f45c52c@gmail.com>
Bytes: 4308

On 06Mar2024 15:12, Jacob Kruger <jacob.kruger.work@gmail.com> wrote:
>So, this does not make sense to me in terms of the following snippet 
>from the official python docs page:
>https://docs.python.org/3/faq/programming.html
>
>"In Python, variables that are only referenced inside a function are 
>implicitly global. If a variable is assigned a value anywhere within 
>the function’s body, it’s assumed to be a local unless explicitly 
>declared as global."
>
>So, I would then assume that if I explicitly include a variable name 
>inside the global statement, then even just assigning it a new value 
>should update the variable in the global context, outside the 
>function?

Yes. Note that the "global" namespace is the module in which the 
function is defined.

     x = 1

     def f(n):
         global x
         x += n

This updates the `x` global variable in the module where `f` was 
defined.

If you import `f` and use it in another module it will _still_ update 
`x` in the original module namespace.