This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello, Bob! | |
Goodbye, Bob! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def say_hello(name): | |
print(f'Hello, {name}!') | |
def say_goodbye(name): | |
print(f'Goodbye, {name}!') | |
def say_to_bob(fun): | |
fun('Bob') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Inside foo: 5 | |
NameError: 'more_specific' is not defined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
less_specific = 5 | |
def foo(): | |
more_specific = 2 | |
print('Inside foo:', less_specific) | |
foo() | |
print(more_specific) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
After bleep: 5 | |
After oof: 20 | |
After bop: 20 | |
Globally: 30 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def foo(): | |
i_am_non_local = 5 | |
def bleep(): | |
i_am_non_local = 10 | |
def oof(): | |
nonlocal i_am_non_local | |
i_am_non_local = 20 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i_am_global = 5 | |
def foo(): | |
global i_am_global | |
i_am_global = 10 | |
print(i_am_global) | |
foo() | |
print(i_am_global) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# first print => 10 | |
# second print => 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i_am_global = 5 | |
def foo(): | |
i_am_global = 10 | |
print(i_am_global) | |
foo() | |
print(i_am_global) | |
# outputs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# scopes.py | |
# Scope A | |
a = 1 | |
b = 16 | |
def outer(): | |
# Scope B | |
c = 24 | |
d = 'Hello, World!' |