Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Very new to python - help!
#1
Hi all,

I'm just going through the "automate the boring stuff" workbook chapter 4. I cannot get my head around how "bacon local" is printed first? (Any help greatly appreciated!)

def spam():
    eggs = 'spam local'
    print(eggs)  # Prints 'spam local'

def bacon():
    eggs = 'bacon local'
    print(eggs)  # Prints 'bacon local'
    spam()
    print(eggs)  # Prints 'bacon local'

eggs = 'global'
bacon()
print(eggs)  # Prints 'global'
OUTPUT

Output:
bacon local spam local bacon local global
Gribouillis write Jun-01-2026, 07:38 PM:
Please post all code, output and errors (it it's 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 could ask Python to trace every statement that it executes. Assuming the program is named boring.py, if we execute it with the command
Output:
python3 -m trace --trace boring.py
we get the following output which gives the detail of every statement executed by the Python interpreter
Output:
--- modulename: boring, funcname: <module> boring.py(1): def spam(): boring.py(5): def bacon(): boring.py(11): eggs = 'global' boring.py(12): bacon() --- modulename: boring, funcname: bacon boring.py(6): eggs = 'bacon local' boring.py(7): print(eggs) # Prints 'bacon local' bacon local boring.py(8): spam() --- modulename: boring, funcname: spam boring.py(2): eggs = 'spam local' boring.py(3): print(eggs) # Prints 'spam local' spam local boring.py(9): print(eggs) # Prints 'bacon local' bacon local boring.py(13): print(eggs) # Prints 'global' global
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
bacon() is the first function you call in your program. print(eggs) inside of bacon() is the first print statement encountered. The variable eggs is assigned the str 'bacon local', so 'bacon local' is the first thing to be printed.

Why are you confused? Is it confusion over the order of execution or confusion about the value of eggs? Or is it some kind of cognitive dissonance caused by a variable named "eggs" being assigned a value of "local bacon"?
Reply
#4
What's all this Monty Python stuff? Spam? Ughhh!

The magic abbreviation here is LEGB I believe, no nothing to do with new laws banning gays in Ghana.

Locals, Environment, Global, Builtin is the order in which the Python searches for a variable or function of any given name.

# variable
eggs = "devilled"

# can't have a function and a variable of the same name, so add a g to eggs
# will print the local variable [b]eggs[/b], because it is closest, it is in the function, it is L
def egggs():
    eggs = "pickled"
    print(eggs)

# will print the environment variable eggs because it is E, and there is no Local variable eggs
def egges():
    print(eggs)
I never need to use G, Global variables. I think they are not recommended. Maybe someone here can give a good example of G.

Everyone uses B, builtins all the time!

def circle(radius):    
    import math
    area = math.pi * radius**2
    return area

print(f'The area of a circle of radius 1 is {circle(1)}')
Gives:

Output:
The area of a circle of radius 1 is 3.141592653589793
Think LEGB!
Reply


Forum Jump:

User Panel Messages

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