How do I prevent macros from polluting the global namespace via import? #2394
-
As title, I just wrote a macro that requires another namespace imported, but I don't want it to be visible to end user. EDIT: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Not so. Python allows The way to import in a macro expansion is to use a gensym: (defmacro m []
(setv math (hy.gensym))
`(do
(import math :as ~math)
((. ~math sqrt) 2)))
(print (m)) ; => 1.4142135623730951
(print math) ; => NameError: name 'math' is not defined |
Beta Was this translation helpful? Give feedback.
Not so. Python allows
import
wherever a statement is allowed, so long as it's notimport *
.The way to import in a macro expansion is to use a gensym: