May 28, 2014

Interesting points of Python

After 4 months learning and its ecosystem I felt the following amazing things in Python:
(created or borrowed from other language)

1. List comprehension
Quick and beauty way to work with array of elements:
>>> vec = [2, 4, 6]
>>> [3*x for x in vec]
[6, 12, 18]
More: https://docs.python.org/3.4/tutorial/datastructures.html#list-comprehensions

2. Decorator
A function, which wraps another function, is called "decorator", enable us to write:
@app.route('/')
def hello_world():
    return 'Hello World!'

More: http://www.jeffknupp.com/blog/2013/11/29/improve-your-python-decorators-explained

3. Iterator & generator
Good for memory allocation and nice syntax.
https://speakerdeck.com/pyconslides/transforming-code-into-beautiful-idiomatic-python-by-raymond-hettinger-1

4. List unpacking and indexing

5. Named-arguments in functions
It's make the code more readable, without length like Objective-C

6. Collections
defaultdict, namedtuple, ... are our friends!

7. Context manager
It's "with" structure.

More reference:
http://stackoverflow.com/questions/101268/hidden-features-of-python
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html
http://www.cs.cmu.edu/~nschneid/pythonathan/

No comments:

Post a Comment

New comment