100 Days of Code - Scopes and Number (Day12)

Namespaces : Local vs Global Scope

1
2
3
4
5
6
7
8
9
enemies = 1

def increase_enemies():
enemies = 2
print(f"enemies inside a function {enemies}")
increase_enemies()

print(f"enemies outside a function {enemies}")

Local Scope

Local Scope exists within functions.

1
2
3
4
5
6
def increase_enemies():
enemies = 2
print(f"enemies inside a function {enemies}")
increase_enemies()
print(enemies) # enemies is not defined

Does Python have block scope

There is not such thing as block scope in Python.

1
2
3
4
5
if true :
new_enemy = 'Skeleton'

print(new_enemy) # 這樣是可以的,因為沒有 block scope

How to modify a Global Variable

1
2
3
4
5
6
7
8
9
enemies = 1

def increase_enemies():
global enemies # takes global enemies into the function.
enemies += 1
print(f"enemies inside a function {enemies}")

increase_enemies()
print(enemies)

Python Constants and Global Scope

Global constants are variables which you define and you never planning on changing it ever again.
Usually using uppercase to represent it.