Lesson 35 of 55
9 mins readPython Macro Hygiene & esc()
In Plain English
By default, Julia macros are hygienic: local variables declared inside a macro are automatically renamed with unique gensym symbols to avoid clashing with caller variables. Use esc() to access caller variables intentionally.
Deep Dive: How It Works
Hygiene System: Macros automatically rename inner variables (e.g. var"#123#x") to prevent accidental shadowing.
esc(): Marks an expression as unhygienic, meaning it will be evaluated in the caller environment context rather than the macro definition context.
Core Rules to Remember

Automatic Hygiene: Protects caller scopes from unexpected variable overrides.

esc() for Caller Symbols: Wrap inputs in esc(expr) when they need to resolve to caller variables.
Live Interactive Example
Hit Run Code to see it liveHygienic Macros vs esc()
Python 3.12
1
2
3
4
5
6
7
Output Console
Click "Run Code" to view the rendered output.
How it works: Without esc(), var_name would be hygienically renamed and inaccessible.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Set Target Variable with esc
Use `@set_var status_code 200`.
Print `"Status: $status_code"`.
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.