Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
def statement
#1
def sec1():
    a = 3
def sec2():
    print (a)
    
sec1()
sec2()
is there a way to use 'a' outside of sec1 or is it locked to that specific section
(sorry im not sure what 'a' would be called normally called)
Yoriz write Aug-06-2021, 06:03 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
You need to learn about "scope". The scope rules of Python tell you all about who can see a variable based on where it is assigned.

Variable scope is defined as Local, Enclosed, Global or Built-in. Built-in is a special scope for all the standard Python libraries. This lets you use functions and variables from the standard libraries without having to import anything. Global is for anything defined in a file (module) but not inside of a function. Local is for variables defined inside of a function. Enclosed is something most people don't use.

There is a "precedence" of scope. The Local scope can see variables defined in the Enclosed, Global scope and Built-in Scope. Enclosed scope can see variables defined in Global and Built-in scopes. Global can see variables defined in the Built-in scope. You will sometimes see this scope order of precedence referred to as LEGB.
abc = ['a', 'b', 'c']  # This is in the Global scope

def func():
    abc[0] = 'A'  # This is Local socpe, but it can see abc defined in Global scope
    print(len(abc))  # Print and len are defined in the built-in scope.  Also visible here
    c = abc[2]  # This variable is defined in the local scope.  Cannot be seen outside this func()

def other_func():
    print(c) # c is not defined in this scope.  Will raise an exception
Python does have a cheat to get around the local scope restriction. If you declare a local variable to be "global", Python assigns the variable in the global scope.
abc = ['a', 'b', 'c']  # This is in the Global scope
c = None

def func():
    global c  # Tells Python to use "c" from the global scope
    c = abc[2]  # Sets the global variable c

def other_func():
    print(c) # Do not need global here since there is no variable assignment

other_func()
func()
other_func()
Output:
None c
The "global" keyword should be used sparingly if at all. Code that makes extensive use of global variables is difficult to debug. It is very easy to forget to use the "global" declaration and accidentally assign a to a local variable, and it is difficult to find all the places a global variable might be changed.

Instead of using lots of global variables you should write your functions to accept arguments and return values.
abc = ['a', 'b', 'c']  # This is in the Global scope

def func():
    return abc[2]

def other_func(arg):
    print(arg)

other_func(func())
Output:
c
Reply
#3
In addition to what @deanhystad already explained about scope, using global, etc. - another approach would be to have a, sec1 and sec2 as attributes of a class, assuming that they are somehow related and it make sense to be a class

class Spam:
    def sec1(self):
        self.a = 3
    def sec2(self):
        print(self.a)

spam = Spam()
spam.sec1()
spam.sec2()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020