From ddc56a1e78528fa5dd3e1db1353fadbfbf8579a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matth=C3=ADas=20P=C3=A1ll=20Gissurarson?= Date: Sat, 6 Dec 2014 12:13:57 +0000 Subject: [PATCH] Added explanation on Hy <-> Python interop --- docs/tutorial.rst | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/docs/tutorial.rst b/docs/tutorial.rst index dd71d26..664f6d4 100644 --- a/docs/tutorial.rst +++ b/docs/tutorial.rst @@ -483,6 +483,61 @@ In Hy: [address (models.TextField)] [notes (models.TextField)]]) +Hy <-> Python interop +================= + +By importing Hy, you can use Hy directly from Python! + +If you save the following in greetings.hy + +.. code-block:: clj + + (defn greet [name] (print "hello from hy," name)) + +Then you can use it directly from python, by importing hy before importing +the module. In Python:: + + import hy + import greetings + + greetings.greet("Foo") + +You can also declare a function in python (or even a class!) and use it in Hy! + +If you save the following in greetings.py in Python:: + + def greet(name): + print( "hello, %s" % (name)) + +You can use it in Hy: + +.. code-block:: clj + + (import greetings) + (.greet greetings "foo") + +To use keyword arguments, you can use in greetings.py:: + + def greet(name, title="Sir"): + print("Greetings, %s %s" % (title,name)) + + +.. code-block:: clj + + (import greetings) + (.greet greetings "Foo") + (.greet greetings "Foo" "Darth") + (apply (. greetings greet) ["Foo"] {"title" "Lord"}) + +Which would output: + + Greetings, Sir Foo + + Greetings, Darth Foo + + Greetings, Lord Foo + + Protips! ========