Little nuggets in Clojure and Python
Sat Jun 27 2015
#clojure#python
With any new language, as you proceed to use it and learn, you figure out ways to do certain things. For e.g., often times I want to know what the type a certain result or a variable is.
It’s a good thing both Clojure and Python have a REPL.
In clojure, if you wanted to find the type or class of something, you would go:
claws.core=> (map * [3 4] [1 2])
(3 8)
claws.core=> (class (map * [3 4] [1 2]))
clojure.lang.LazySeq
claws.core=> (type (map * [3 4] [1 2]))
clojure.lang.LazySeq
What if I wanted to find the functions in a namespace[1]? From StackOverflow:
claws.core=> (keys (ns-publics 'clojure.tools.cli))
(summarize parse-opts cli)
If you wanted to find the type of a class or a variable in python, you would use python’s builtin function type thusly:
>>> import boto.ec2 as ec2
>>> conn=ec2.connect_to_region('us-east-1')
>>> type(conn)
<class 'boto.ec2.connection.EC2Connection'>
And if you wanted to get the modules, functions etc., of a class in python, you would use inspect[2]:
>>> import inspect
>>> inspect.getmembers(inspect, predicate=inspect.isbuiltin)
[('currentframe', <built-in function _getframe>)]
[1] More info about clojure namespaces is available here.
[2] Inspect module’s documentation is here.