Clojure Namespaces for Python Programmers

My mentor Colin wrote one of the most popular posts on the 8th Light blog, on the somewhat complex process of requiring Clojure libs. It’s not a stretch to give it some of the credit for my interest in Clojure: I came close to giving up in frustration the first time I tried to import a library in a Clojure REPL. It was so easy in Python, and there I was, suddenly dealing with macros, keywords, quotes, and–heaven forfend!–all those parentheses.

For all the details on namespaces, require, and the ns macro, see Colin’s post, which is still the authoritative source. But if you’re a Python programmer looking for a quick reference, here’s the Python-Clojure Rosetta stone I went looking for the first time I deadpanned into my Clojure REPL.

Import a library

File
1
2
3
import library

library.dostuff()
1
2
3
4
(ns yourproject.core
  (:require library))

(library/dostuff)
REPL
1
2
3
4
In  [1]: import library

In  [2]: library.dostuff()
"did stuff!"
1
2
3
4
5
6
user=> (require 'library)
nil

user=> (library/dostuff)
"did stuff!"
nil

Import everything without qualification

(Note: This practice is usually as ill-advised in Clojure as it is in Python!)

File
1
2
3
from library import *

dostuff()
1
2
3
4
(ns yourproject.core
  (:require [library :refer :all]))

(dostuff)
REPL
1
2
3
4
In  [1]: from library import *

In  [2]: dostuff()
"did stuff!"
1
2
3
4
5
6
user=> (require '[library :refer :all])
nil

user=> (dostuff)
"did stuff!"
nil

Import specific functions

File
1
2
3
4
from library import dostuff, returnthings

dostuff()
returnthings()
1
2
3
4
5
(ns yourproject.core
  (:require [library :refer [dostuff returnthings]]))

(dostuff)
(returnthings)
REPL
1
2
3
4
5
6
7
In  [1]: from library import dostuff, returnthings

In  [2]: dostuff()
"did stuff"

In  [3]: returnthings()
Out [3]: { "foo": 1, "bar": 2 }
1
2
3
4
5
6
7
8
9
user=> (require '[library :refer [dostuff returnthings]])
nil

user=> (dostuff)
"did stuff"
nil

user=> (returnthings)
{ :foo 1 :bar 2 }

Import and rename a library

File
1
2
3
import library as lib

lib.dostuff()
1
2
3
4
(ns yourproject.core
  (:require [library :as lib]))

(lib/dostuff)
REPL
1
2
3
4
In  [1]: import library as lib

In  [2]: lib.dostuff()
"did stuff"
1
2
3
4
5
6
user=> (require '[library :as lib])
nil

user=> (lib/dostuff)
"did stuff"
nil

Import and rename a function

File
1
2
3
from library import dostuff as ds

ds()
1
2
3
4
(ns yourproject.core
  (:require [library :refer [dostuff] :rename {dostuff ds}]))

(ds)
REPL
1
2
3
4
In [1]: from library import dostuff as ds

In [2]: ds()
"did stuff"
1
2
3
4
5
6
user=> (require '[library :refer [dostuff] :rename {dostuff ds}])
nil

user=> (ds)
"did stuff"
nil

All together, now!

File
1
2
3
4
5
import onelibrary, anotherlibrary
import onemorelibrary as oml

from library import returnthings
from library import dostuff as ds
1
2
3
4
5
6
(ns yourproject.core
  (:require [onelibrary
             anotherlibrary
             [onemorelibrary :as oml]
             [library :refer [dostuff returnthings]
                      :rename {dostuff ds}]]))

Comments