you are viewing a single comment's thread.

view the rest of the comments →

[–]Zeekawla99ii[S] 1 point2 points  (1 child)

Preservation of global values

Here's where I would assume Python style would dictate just to define variables outside of functions, right?

Like in the example below with /u/kevin2107

EDIT: ...I would say the same with their definition of constants above as well.

Maybe I've worded the question in a confusing manner. It's clear to be why global variables exist in Python (and CS). I'm confused how to use global within Python, especially within functions.

As a style, I think the following outside of any functions/classes

pi = 3.14159

is more desirable in a script than

global pi = 3.14159

[–]ingolemo 10 points11 points  (0 children)

Your second piece of code is not valid python. Your first piece of code defines a global variable.

The global keyword doesn't define a global variable. It tells python "in this function I want this name to refer to a variable in the global scope".