Anda di halaman 1dari 21

WHAT MAKES PYTHON

SO POPULAR?
Jung-Hoon Rhew
intel.dts.ptm.pdmg

10/7/2010

Jung-Hoon Rhew

Overview
I.
II.
III.
IV.
V.
VI.
VII.
VIII.

What is Python? Brief History


Where is Python Popular?
Is Python getting Popular?
What makes Python so Popular?
Code Examples
Python vs. Ruby Debate
How to Start
Summary

10/7/2010

Jung-Hoon Rhew

I. What is Python - Brief


History

Conception: 1980
Implementation: 1989
Creator: Guido van Rossum @ CWI (once @Google)
Heritage: SETL ABC Python
Named from Monty Pythons Flying Circus
Key Features

Simple and intuitive language construct


Multi-paradigm (Structured, OOP, FP, AOP, )
Dynamically (but strongly) typed with garbage collection
Interpreted but highly scalable and extensible
Spiritual (Zen of Python)

Major Releases

0.9: Feb. 20, 1991 (First)


2.0: Oct. 16, 2000 (2.x branch is still active, most popular)
3.0: Dec. 3, 2008
Jung-Hoon Rhew
10/7/2010

II. Where is Python Popular?

Google

Microsoft

IronPython (Python for .NET)

Finance

Major contributor to Googles engineering success


One of the three official languages along with C++ &
Java
Google App Engine provides Python and Java SDKs only

Proposed by U.S. SEC for use in financial modeling for


transparency (e.g. Pandas library)

Academia

10/7/2010

Computer science (Python as a First Language)


Scientific computing (NumPy, SciPy, NanoHub)
Social science (Python extension in SPSS)
Jung-Hoon Rhew

III. Is Python getting Popular?


Yes: The Growth of Dynamic Languages
(7/28/2010 Active Blog)

The blog offers the following reasons for Pythons high usage and
growth.
Adoption, support, and investment from Google and Microsoft
Strong support and community
Established developer base
Enterprise friendly licensing
Popularity for web applications with prevailing cloud computing
Proposed by the SEC for use in financial modeling

However, these are just an outcome of a more fundamental


10/7/2010
Jung-Hoon Rhew
cause.

IV. What makes Python so


Popular?

Superb productivity in creating, (re)using, sharing,


and maintaining knowledge across different
domains.
Thanks to Pythons letter and spirit that
relentlessly pursue beautiful coding

10/7/2010

The philosophy is established both in its core language construct


and in its community culture
Beautiful = Explicit, Simple, Flat, Sparse, Readable as
summarized in Zen of Python (PEP-20)
Syntax/Semantics is kept simple and intuitive for good citizens
to use easily and correctly.
Culture raises good citizens by promoting good practices while
discouraging bad ones. Idiomatic Python, Style guide (PEP-8)
Bruce Eckel (C++ & Java Guru) explains in Why I love Python
how minimizing clutter improves productivity, and the
relationship between backwards compatibility and programmer
pain.
Jung-Hoon Rhew

IV.0 Read-Time Convenience

Programmers spend >95% of time on

Development productivity, code maintainability,


effectiveness of knowledge sharing, etc. are strongly
correlate to Read-Time Convenience.
In this sense, Python defines what the beautiful code
is.

Reading the surrounding code.

This might not be as much fun as coding in a language


oriented to Write-Time Convenience.

Read-time convenience requires the language to be

10/7/2010

Explicit, Simple, Flat, and Sparse because Readability counts.


These characteristics may compromise convenience or fun of
writing code in local scope.
However, it promotes convenience or fun of writing code into
a scalable, maintainable structure.
Jung-Hoon Rhew

IV.1. Python Culture

While offering choices in coding, the Python philosophy rejects


exuberant syntax, such as in Perl, in favor of a sparser, lesscluttered grammar. Python developers expressly promote a
particular "culture", favoring language forms they see as
"beautiful", "explicit", and "simple"
To describe something as clever is NOT considered a compliment
in the Python culture." Python's philosophy rejects the Perl "
there is more than one way to do it" approach to language
design in favor of "there should be oneand preferably only one
obvious way to do it." Alex Martelli
Python developers eschew premature optimization, and
moreover, reject patches to non-critical parts of CPython which
would offer a marginal increase in speed at the cost of clarity.
Python is sometimes described as "slow". However, most
problems and sections of programs are not speed critical. When
speed is a problem, Python programmers tend to try using a JIT
compiler or rewriting the time-critical functions in C/C++.

10/7/2010

Jung-Hoon Rhew

IV.2. Zen of Python

Author: Tim Peters


Guiding principles but open to interpretation
Part 1
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

10/7/2010

Beautiful is better than ugly.


Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
Jung-Hoon Rhew

IV.2. Zen of Python Cont.

Part 2
12.
13.
14.
15.
16.
17.
18.
19.

10/7/2010

In the face of ambiguity, refuse the temptation to


guess.
There should be oneand preferably only one
obvious way to do it.
Although that way may not be obvious at first unless
you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad
idea.
If the implementation is easy to explain, it may be a
good idea.
Namespaces are one honking great idealet's do
more of those!
Jung-Hoon Rhew

10

IV.3. Master Yodas Lesson

10/7/2010

Yoda: Code! Yes. A programmers strength


flows from code maintainability. But beware
of Perl. Terse syntax more than one way to
do it default variables. The dark side of
code maintainability are they. Easily they
flow, quick to join you when code you write. If
once you start down the dark path, forever
will it dominate your destiny, consume you it
will.
Luke: Is Perl better than Python?
Yoda: No no no. Quicker, easier, more
seductive.
Luke: But how will I know why Python is
better than Perl?
Yoda: You will know. When your code you try
to read six months from now.
Jung-Hoon Rhew

11

V.1. Code Examples - Readable


def list_to_0_index(lst):
""" # docstring start
Returns a list that contains the 0-index of the first
occurrence of elements in the list lst.
So the list x = [0, 1, 4, 2, 4, 1, 0, 2] is
transformed into y = [0, 1, 2, 3, 2, 1, 0, 3].
>>> x = [0, 1, 4, 2, 4, 1, 0, 2] # doctest
>>> list_to_0_index(x)
[0, 1, 2, 3, 2, 1, 0, 3]
""" # docstring end
return [lst.index(i) for i in lst] # list comprehension
if __name__ == "__main__": # command-line mode
import doctest
# import doctest module
doctest.testmod()
# run doc test

Compare the readability of list_to_0_index to C++, Java, and


Haskell counterparts at
http://rgrig.blogspot.com/2005/11/writing-readable-code.html
10/7/2010

Jung-Hoon Rhew

12

V.2. Code Examples - Pythonic


The following Easter egg displays Zen of Python in the interpreter
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.

Namespaces are one honking great idea -- let's do more of those!


this module creates Zen of Python by decoding ROT13 encrypted
string stored in this.s.
The ROT13 mapping is stored in this.d.
Below are both non-pythonic codes and a pythonic one (preferably only one obvious
way).
# Non-pythonic code 1

# Non-pythonic code 2

result = []
for c in this.s:
if c in this.d:
result.append(this.d[c])
else:
result.append(c)
print ''.join(result)

print ''.join([this.d[c] if c in this.d else c


for c in this.s])

10/7/2010

# Pythonic code using built-in dict.get method


and generator expression
print ''.join(this.d.get(c,c) for c in this.s)
Jung-Hoon Rhew

13

V.3. Code Examples Namespaces

Zen of Python rule #19


Namespaces are one honking great idea -- let's do more of those!
The following directory structure and corresponding import statements
show how to create and manage modules and packages.
package/
__init__.py
# makes the directory recognized as a namespace
module1.py
# file name is a namespace
subpackage/
__init__.py
module2.py
# module2 contains object name

>>> import package.module1


>>> from package.subpackage import module2 as mymodule2
>>> from package.subpackage.module2 import name

So easy even Grandma can do!


10/7/2010

Jung-Hoon Rhew

14

VI. Python vs. Ruby Debate

Why do I bring this up?

The starting question was Should I learn Python


or Ruby next? from a Perl programmer.
Someone in the thread analyzed 100+ posts.

Quite often, promoting Python comes down to this


debate.
The debate is among daily programmers who are
experienced in either language or both.
Examination of the debate reveals why Python is great
not just over Ruby but in general.

50% voted for Python


30% voted for Ruby
20% voted for learning both

This is not the end of the story.

10/7/2010

Jung-Hoon Rhew

15

VI. Python vs. Ruby Debate Cont.

Disclaimer

Not trying to say dont learn Ruby. Learning other


languages is important if you can afford and they meet
your organizational need (e.g.
Ruby on Rails for web development).

Myths

From Equivocal side


Ruby has more powerful features but Python has more

libraries.
Both languages are equivalent or equally capable.

From Ruby side


Ruby provides powerful features Python cant or does with

limitation.
So, Ruby allows more creative ways of doing things and
fun to use.

10/7/2010

Even some of experienced Python programmers are


Jung-Hoon Rhew
swayed by those myths.

16

VI. Python vs. Ruby Debate Cont.

What is Powerful?

Usually a capability that enables different things,


implicitly.
Fits well into Perl mantra more than one way to do it.
Takes a lot of time to (or learn how to) use correctly and
safely.
due to lack of read-time convenience.

Python deliberately avoids or limits such features as


stated in Python's Design Philosophy.
Borrow ideas from elsewhere whenever it makes

(Pythonic) sense: read-time convenience over writetime convenience.


Do one thing well (The "UNIX philosophy").

What is Creative?

10/7/2010

It is NOT creativity favoring personal pleasure of coding


over responsibility to the public.
Jung-Hoon Rhew

17

VI. Python vs. Ruby Debate Cont.

Still not convinced?


Let me remind you of the evidences

Major contributor to Googles engineering success as one of


their three official languages along with C++ & Java
Widely adopted in various domains; scientific computing,
finance, statistics, GUI, DB, web applications, and so on.
Python has a lot more (~4x) quality libraries across different
domains than Ruby or any other in the list thanks to its
amazing productivity.

10/7/2010

Jung-Hoon Rhew

18

VII. How to Start

Guidos Python Tutorial. (~60 pages light-weight reading)


Understand Pythons design philosophy (including the
Zen).
Get used to Idiomatic Python.
Better to follow Style guide (PEP-8).
Read good books on Python
Search web for references and video clips (Official Python
docs, Google python class, PyCon talks, etc.)
You can become a capable (Python) programmer much
quicker.

10/7/2010

Jung-Hoon Rhew

19

VIII. Summary

Life of three smart programmers


who disregarded Python

Life of their simple coworker


who happened to pick up Python
they had kicked away
10/7/2010

Jung-Hoon Rhew

20

10/7/2010

Jung-Hoon Rhew

21

Anda mungkin juga menyukai