Skip to content

Commit 0464eed

Browse files
authored
docs/pyinterop.rst: elucidate syntax around referencing things within modules (#1263)
I find the initial writeup a bit terse, and also not clear and somewhat confusing for me. I hope you will accept this edit; I think this is an improvement, and helps elucidate and call out the one thing that confused me: namely, how referencing stuff within a class effectively creates its own namespace, something that took this used-to-Clojure brain some time to grok (making this PR was also part of my process). Thank you for Basilisp!
1 parent 921846a commit 0464eed

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

docs/pyinterop.rst

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,47 @@ It is also possible to refer all module members into the namespace using ``:refe
104104
Referencing Module Members
105105
--------------------------
106106

107-
Once a Python module is imported into the current Namespace, it is trivial to reference module members directly.
108-
References to Python module members appear identical to qualified Basilisp Namespace references.
109-
Class constructors or other callables in the module can be called directly as a standard Basilisp function call.
110-
Static members and class members can be referenced by adding the class name to the (potentially) qualified symbol namespace, separated by a single ``.``.
107+
Once a Python module is imported into the current Namespace, it is trivial to reference things (if a bit unintuitive for Class members if you're used to Clojure syntax; but more on that later) within the module.
108+
109+
References to Python module top-level members are as expected, with the namespace at the front, followed by a ``/``, and then the name of the member:
110+
111+
.. code-block:: python
112+
113+
# src/boo.py
114+
global_var = "boo"
115+
116+
def module_method():
117+
...
118+
119+
class BooClass:
120+
class_var = "BooClass class variable!"
121+
...
122+
123+
@classmethod
124+
def some_class_method(cls):
125+
return f"hello from {cls}!"
111126
112127
.. code-block:: clojure
113128
114-
(import datetime)
115-
(datetime.datetime/now) ;;=> #inst "2020-03-30T08:56:57.176809"
129+
(import src.boo)
130+
131+
src.boo/global-var ;; => "boo"
132+
src.boo/BooClass ;; => <class 'test.BooClass'>
133+
134+
;; top-level callables within the module can be called as you would a standard Basilisp function call
135+
(src.boo/module-method)
136+
(src.boo/BooClass)
137+
138+
For referencing members within classes, Basilisp expects that you tack on the class name with a leading ``.`` to the (potentially) qualified namespace symbol:
139+
140+
.. code-block:: clojure
141+
142+
(import src.boo)
143+
144+
(src.boo.BooClass/class-var) ;; => "BooClass class variable!"
145+
(src.boo.BooClass/some-class-method) ;; => "hello from <class 'src.test.BooClass'>!"
146+
147+
Notice that for these cases the class (name) effectively becomes a namespace of its own, even if it is not defined in a separate file. This is unlike Clojure.
116148

117149
.. _accessing_object_methods_and_props:
118150

0 commit comments

Comments
 (0)