If you are looking for a namespace that is currently executing code at runtime, you can simply look at the value of clojure.core/*ns* :
user> (defn which-ns? [] (str *ns*)) user> (which-ns?) "user" user> (ns user2) user2> (which-ns?) "user2"
If you are looking for a file in which var or namespace was defined, then the location of the source code that you are referencing is stored by the compiler as metadata in var when evaluating the form def :
user> (defn foo [x] (inc x)) user> (meta #'foo) {:arglists ([x]), :ns #<Namespace user>, :name foo, :line 1, :file "NO_SOURCE_FILE"}
"NO_SOURCE_FILE" is that you evaluate the form entered in the REPL. If you are evaluating code from a source file, then :file will indicate the path to the source file.
source share