From b4b3ab89f7f8042b7ad0c1c03c58fa0422e3f596 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Thu, 9 May 2013 16:00:30 -0500 Subject: [PATCH] Documenting classes! --- docs/tutorial.rst | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docs/tutorial.rst b/docs/tutorial.rst index 99f18fc..75df541 100644 --- a/docs/tutorial.rst +++ b/docs/tutorial.rst @@ -433,6 +433,46 @@ The Hy equivalent: (import pprint) (pprint.pprint (, foo bar args kwargs))) +Finally, of course we need classes! In python we might have a class +like:: + + class FooBar (object): + def __init__(self, x): + self.x = x + + def get_x(self): + return self.x + + +In Hy: + +.. code-block:: clj + + (defclass FooBar [object] + [[--init-- + (fn [self x] + (setv self.x x))] + + [get-x + (fn [self] + self.x)]]) + + +You can also do class-level attributes. In Python:: + + class Customer(models.Model): + name = models.CharField(max_length=255) + address = models.TextField() + notes = models.TextField() + +In Hy: + +.. code-block:: clj + + (defclass Customer [models.Model] + [[name (kwapply (models.CharField) {"max_length" 255})] + [address (models.TextField)] + [notes (models.TextField)]]) Protips!