Skip to content

Commit

Permalink
Project restructured
Browse files Browse the repository at this point in the history
  • Loading branch information
YAtOff committed Jan 16, 2014
1 parent 8204d73 commit 171263d
Show file tree
Hide file tree
Showing 8 changed files with 627 additions and 556 deletions.
36 changes: 36 additions & 0 deletions controls.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Control structures
==================

Chaining comparison operators
-----------------------------

>>> x = 5
>>> 1 < x < 10
True
>>> 10 < x < 20
False
>>> x < 10 < x*10 < 100
True
>>> 1 <= x <= 9
True
>>> 4 < x == 5
True


The for...else syntax
---------------------

>>> def find(seq, val):
... for i in seq:
... if i == val:
... print "found"
... break
... else:
... print "not found"
>>> find([1, 2, 3], 2)
found
>>> find([1, 2, 3], 5)
not found

The "else" block will be normally executed at the end of the for loop, unless
the break is called (see http://docs.python.org/ref/for.html).
66 changes: 66 additions & 0 deletions functions.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
Functions
=========

Be careful with mutable default arguments
-----------------------------------------

>>> def foo(x=[]):
... x.append(1)
... print x
...
>>> foo()
[1]
>>> foo()
[1, 1]
>>> foo()
[1, 1, 1]

Instead, you should use a sentinel value denoting "not given" and replace with
the mutable you'd like as default:

>>> def foo(x=None):
... if x is None:
... x = []
... x.append(1)
... print x
>>> foo()
[1]
>>> foo()
[1]


Partial functions
-----------------

>>> from functools import partial
>>> bound_func = partial(range, 0, 10)
>>> bound_func()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> bound_func(2)
[0, 2, 4, 6, 8]


Operators can be called as functions
------------------------------------

>>> from operator import add
>>> print reduce(add, [1, 2, 3, 4, 5, 6])
21


Manipulating Recursion Limit
----------------------------

We can limit it to prevent a stack overflow caused by infinite recursion by
getting or setting the maximum depth of recursion with `sys.getrecursionlimit()`
& `sys.setrecursionlimit()`.


"Unpacking" to function parameters
----------------------------------

>>> def foo(a, b, c):
... print a, b, c
>>> bar = (1, 2, 3)
>>> foo(*bar)
1 2 3
13 changes: 10 additions & 3 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
Welcome to Python quirks's documentation!
=========================================

Contents:
>>> import __hello__
Hello world...

And here are some more hidden python futures:

.. toctree::
:maxdepth: 2
:maxdepth: 3

quirks
controls
functions
sequences
types
tricks

Indices and tables
==================
Expand Down
Loading

0 comments on commit 171263d

Please sign in to comment.