Add context manger / 'with' statement to tutorial
This commit is contained in:
parent
3f362bc318
commit
499ec7697c
@ -324,6 +324,18 @@ Comments start with semicolons:
|
|||||||
; (print "but this will not")
|
; (print "but this will not")
|
||||||
(+ 1 2 3) ; we'll execute the addition, but not this comment!
|
(+ 1 2 3) ; we'll execute the addition, but not this comment!
|
||||||
|
|
||||||
|
Python's context managers ('with' statements) are used like this:
|
||||||
|
|
||||||
|
.. code-block:: clj
|
||||||
|
|
||||||
|
(with [f (file "/tmp/data.in")]
|
||||||
|
(print (.read f)))
|
||||||
|
|
||||||
|
which is equivalent to::
|
||||||
|
|
||||||
|
with file("/tmp/data.in") as f:
|
||||||
|
print f.read()
|
||||||
|
|
||||||
And yes, we do have lisp comprehensions! In Python you might do::
|
And yes, we do have lisp comprehensions! In Python you might do::
|
||||||
|
|
||||||
odds_squared = [
|
odds_squared = [
|
||||||
@ -363,7 +375,7 @@ In hy, you could do these like:
|
|||||||
; (8, 'A'), (8, 'B'), (8, 'C'), (8, 'D'), (8, 'E'), (8, 'F'), (8, 'G'), (8, 'H')]
|
; (8, 'A'), (8, 'B'), (8, 'C'), (8, 'D'), (8, 'E'), (8, 'F'), (8, 'G'), (8, 'H')]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Protips!
|
Protips!
|
||||||
========
|
========
|
||||||
|
|
||||||
@ -409,6 +421,7 @@ TODO
|
|||||||
====
|
====
|
||||||
|
|
||||||
- How do I define classes?
|
- How do I define classes?
|
||||||
|
- How do I use context managers?
|
||||||
- Blow your mind with macros!
|
- Blow your mind with macros!
|
||||||
- Where's my banana???
|
- Where's my banana???
|
||||||
- Mention that you can import .hy files in .py files and vice versa!
|
- Mention that you can import .hy files in .py files and vice versa!
|
||||||
|
Loading…
x
Reference in New Issue
Block a user