2. 2 www.ExigenServices.com
• Who‟s using it?
• Why?
• Overview
• Kind of magic: special keywords
• Decorators
• Samples
• Zen
Presentation index
3. 3 www.ExigenServices.com
Who’s using it?
Sourceforge, Fedora
community
TurboGears
Google, Yahoo, Zope,
Battlefield 2, Vampires:
TM, Civilization 4,
Blender, SGI, Red Hat,
Nokia, Caligari, ABN
AMRO Bank, NASA,
Thawte consulting, IBM,
……
OVER 9000!
Python
Instagram, Pinterest,
Mozilla, The Guardian,
The New York Times, The
Washington Post, Quora
Django
5. 5 www.ExigenServices.com
Why Python, not Ruby, Smalltack, Brainf*ck?
• Small core
• 29 keywords, 80 built-in functions
• Elegant syntax
• Embeddable to everything you want
• True cross platform
• Absolutely free
• Extendable (via C++ modules)
• Rich standard library
• Binding almost to everything
6. 6 www.ExigenServices.com
• Fast logic prototyping
• It automates for you mechanical work
• Improves you mind
• Force you to write clear code (on other langs also, sad but true)
• Web development
• Desktop development
• Access to system internal features (WinAPI, Dbus)
• You can find tool for everything.
Why should I spend my time on it instead of beer?
7. 7 www.ExigenServices.com
• Easier than Pascal
• Better than Basic
• Slimmer than PHP
• Prettier than Ruby (empty? empty! empty!! empty!!!!!)
• Does not suffer of abundance of parentheses (((hello) (,) (Lisp!)))
• Pointers? What is it? Ahhhh, it is C++!
• More much dynamic than Java (hello, dynamic typing)
• Improves karma and makes you happy
Wait, what does it means? Really, why?
10. 10 www.ExigenServices.com
• Dynamic (duck) typing
• Variety of basic data types
• Hierarchy marked by indentation
• Code may be grouped to modules
• Exception infrastructure
• Advanced features like generators and list comprehension
• Garbage collector and automatic memory management
Overview: Heap, part 1
11. 11 www.ExigenServices.com
• Function marked by keyword „def‟
• Classes marked by keyword „class‟
• String are immutable
• Special keywords surrounded by
double underscore
• Variables not marked.
Just assign and use.
• Lambda functions
• Object model
Overview: Heap, part 2
12. 12 www.ExigenServices.com
• Do you like objects?
• Class instance is object
• Class definition is object
too.
• Functions. It is objects also.
• Aren‟t you bored of objects?
Elementary types are objects.
• Types… Ok, you know…
• EVERYTHING IS OBJECT!
Everything is object! No exceptions.
15. 15 www.ExigenServices.com
• There are matter how to declare
class data members: class-
or instance-wide.
• Class data members
Declared inside class body.
Accessible for all instances
• Instance data members
Declared inside constructor.
Accessible only for one instance.
Class introspection: Members declaration
16. 16 www.ExigenServices.com
• Data members
• Public (no special marks)
• Private-by-convention (started with at least one underscore and
finished with not more than 1 underscore)
• Private-by-mangling (started with 2 underscores and finished
with not more than 1 underscore)
Class introspection: Member access
17. 17 www.ExigenServices.com
• Each class member contains
reference to class object
• Class methods (except static)
are unbound
• Instance methods are bound
• Instance methods may be
called directly
• Class methods may be
called indirectly
Class introspection: Method calls
22. 22 www.ExigenServices.com
0 1 2 3
[1, 2, 3, 4]
-4 -3 -2 -1
• Index model:
• Indexation
• Indices started from 0
• Indices may be negative
• Slices
• With positive indices
• With negative indices
(be careful with order!)
• With step
Sequences indexation and slicing
28. 28 www.ExigenServices.com
• Decorator
• A function that takes one argument
(i.e. the function being decorated)
• Returns the same function or a function
with a similar signature
(i.e. something useful)
• Still WTF? Do not bother,
“what is decorator” is one of most
popular Python questions
Decorators
30. 30 www.ExigenServices.com
• Declare the decorator
• Wrap the function
• …
• PROFIT!
• Hey! I want to wrap MY
function!
• Add some magic
• …
• PROFIT again!
Decorators: Wrap the function
34. 34 www.ExigenServices.com
• Simple HTTP server
python -m SimpleHTTPServer 8888
• SMTP server
python -m smtpd -n -c DebuggingServer localhost:25
• CGI server
python -m CGIHTTPServer 9080
• How many bytes in…
zip(
('Byte','KByte','MByte','GByte',TByte'),
(1 << 10*i for i in xrange(5))
)
• Windows clipboard
import win32clipboard
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(text)
win32clipboard.CloseClipboard()
One-, two- and three-line scripts, part 1
35. 35 www.ExigenServices.com
• Decode base64
import base64, sys;
base64.decode(
open(“encoded.file”, "rb"),
open(“decoded.file”, "wb")
)
• COM
• Test via Python
• Test via VB
One-, two- and three-line scripts, part 2
42. 42 www.ExigenServices.com
• http://docs.python.org/
• http://learnpythonthehardway.org/
• Google Python Days on Youtube
• Coursera:
https://www.coursera.org/course/interactivepython
https://www.coursera.org/course/programming1
• EdX:
https://www.edx.org/courses/MITx/6.00x/2012_Fall/about
• Google, do you speak it?!
• Stackoverflow
• http://www.tornadoweb.org/
• http://www.pylonsproject.org/
• https://www.djangoproject.com/
• http://flask.pocoo.org/
• http://wiki.python.org/moin/PythonDecoratorLibrary
Additional resources
43. 43 www.ExigenServices.com
>>> import this
• 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.
to be continued…
The Zen of Python
44. 44 www.ExigenServices.com
>>> import this
• In the face of ambiguity, refuse the temptation to guess.
• There should be one-- and 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 idea -- let's do more of those!
The Zen of Python, part 2