Tweak Python interop section a bit.

This commit is contained in:
Berker Peksag 2014-12-06 18:10:35 +02:00
parent 840eff8777
commit 5034b5a918

View File

@ -484,11 +484,11 @@ In Hy:
[notes (models.TextField)]]) [notes (models.TextField)]])
Hy <-> Python interop Hy <-> Python interop
================= =====================
By importing Hy, you can use Hy directly from Python! By importing Hy, you can use Hy directly from Python!
If you save the following in greetings.hy If you save the following in ``greetings.hy``:
.. code-block:: clj .. code-block:: clj
@ -497,46 +497,45 @@ If you save the following in greetings.hy
Then you can use it directly from python, by importing hy before importing Then you can use it directly from python, by importing hy before importing
the module. In Python:: the module. In Python::
import hy import hy
import greetings import greetings
greetings.greet("Foo") greetings.greet("Foo")
You can also declare a function in python (or even a class!) and use it in Hy! 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:: If you save the following in ``greetings.py`` in Python::
def greet(name): def greet(name):
print( "hello, %s" % (name)) print("hello, %s" % (name))
You can use it in Hy: You can use it in Hy:
.. code-block:: clj .. code-block:: clj
(import greetings) (import greetings)
(.greet greetings "foo") (.greet greetings "foo")
To use keyword arguments, you can use in greetings.py:: To use keyword arguments, you can use in ``greetings.py``::
def greet(name, title="Sir"):
print("Greetings, %s %s" % (title,name))
def greet(name, title="Sir"):
print("Greetings, %s %s" % (title,name))
.. code-block:: clj .. code-block:: clj
(import greetings)
(.greet greetings "Foo")
(.greet greetings "Foo" "Darth")
(apply (. greetings greet) ["Foo"] {"title" "Lord"})
Which would output: (import greetings)
(.greet greetings "Foo")
(.greet greetings "Foo" "Darth")
(apply (. greetings greet) ["Foo"] {"title" "Lord"})
Which would output::
Greetings, Sir Foo Greetings, Sir Foo
Greetings, Darth Foo Greetings, Darth Foo
Greetings, Lord Foo Greetings, Lord Foo
Protips! Protips!