background preloader

Advanced Topics

Facebook Twitter

Deep Thoughts by Raymond Hettinger. If you aren’t wowed by Python’s super() builtin, chances are you don’t really know what it is capable of doing or how to use it effectively.

Deep Thoughts by Raymond Hettinger

Much has been written about super() and much of that writing has been a failure. This article seeks to improve on the situation by: providing practical use casesgiving a clear mental model of how it worksshowing the tradecraft for getting it to work every timeconcrete advice for building classes that use super()favoring real examples over abstract ABCD diamond diagrams.

The examples for this post are available in both Python 2 syntax and Python 3 syntax. Using Python 3 syntax, let’s start with a basic use case, a subclass for extending a method from one of the builtin classes: class LoggingDict(dict): def __setitem__(self, key, value): logging.info('Settingto %r' % (key, value)) super(). This class has all the same capabilities as its parent, dict, but it extends the __setitem__ method to make log entries whenever a key is updated. Search Order. How can I create a stand-alone binary from a Python script? You don’t need the ability to compile Python to C code if all you want is a stand-alone program that users can download and run without having to install the Python distribution first.

How can I create a stand-alone binary from a Python script?

There are a number of tools that determine the set of modules required by a program and bind these modules together with a Python binary to produce a single executable. One is to use the freeze tool, which is included in the Python source tree as Tools/freeze. Oop - What is a metaclass in Python. Understanding Python's "with" statement. Fredrik Lundh | October 2006 | Originally posted to online.effbot.org Judging from comp.lang.python and other forums, Python 2.5’s new with statement (dead link) seems to be a bit confusing even for experienced Python programmers.

Understanding Python's "with" statement

As most other things in Python, the with statement is actually very simple, once you understand the problem it’s trying to solve. Consider this piece of code: set things up try: do something finally: tear things down Here, “set things up” could be opening a file, or acquiring some sort of external resource, and “tear things down” would then be closing the file, or releasing or removing the resource. If you do this a lot, it would be quite convenient if you could put the “set things up” and “tear things down” code in a library function, to make it easy to reuse. The Python "with" Statement by Example. Python’s with statement was first introduced five years ago, in Python 2.5.

The Python "with" Statement by Example

It’s handy when you have two related operations which you’d like to execute as a pair, with a block of code in between. The classic example is opening a file, manipulating the file, then closing it: with open('output.txt', 'w') as f: f.write('Hi there! ') The above with statement will automatically close the file after the nested block of code. (Continue reading to see exactly how the close occurs.) Here’s another example. This code sample uses a Context object (“cairo context”) to draw six rectangles, each with a different rotation. Cr.translate(68, 68) for i in xrange(6): cr.save() cr.rotate(2 * math.pi * i / 6) cr.rectangle(-25, -60, 50, 40) cr.stroke() cr.restore() A life of coding: Closures in Python. Functional programming - Can you explain closures (as they relate to Python) Python Tutorial: decorators (functions and classes) - 2013.

Decorators "Decoration is a way to specify management code for functions and classes.

Python Tutorial: decorators (functions and classes) - 2013

" ... "A decorator itself is a callable that returns a callable. " - Mark Lutz Function Decorators Let's start with a function in Python. To understand decorators, we need to know the full scope of capabilities of Python functions. Def func(): print "func()" funcObj = func funcObj() Functions can be passed around in the same way other types of object such as strings, integers, lists, etc.

(Python) Decorators. Decorators in Python are bit confusing concept to understand.

(Python) Decorators

It is not a standard feature generally explained in many of the Beginners’ books on Python. But, understanding the basic idea of decorators will help us in making a very good use of it. As the rule always goes, there are certain situations where decorators are highly useful but not always. Decorators is a way of transforming functions and methods at the point where the code is declared. The point of transformation is placed just before the method’s own declaration. To give a more clear idea, we generally put some code within a function, make use of it in other functions by calling it, performing some calculations or processing within the function’s body, and finally return some result or print some output.

Think of a situation where the attributes of the function get modified when the function is actually executed. Charming Python: Decorators make magic easy. Doing a lot by doing very little Decorators have something in common with previous metaprogramming abstractions introduced to Python: they do not actually do anything you could not do without them.

Charming Python: Decorators make magic easy

As Michele Simionato and I pointed out in earlier Charming Python installments, it was possible even in Python 1.5 to manipulate Python class creation without the "metaclass" hook. Decorators are similar in their ultimate banality. All a decorator does is modify the function or method that is defined immediately after the decorator. This was always possible, but the capability was particularly motivated by the introduction of the classmethod() and staticmethod() built-in functions in Python 2.2.

Listing 1. Python 2.4 Decorators — www.drdobbs. Python3 Tutorial: Memoization and Decorators. Definition of Memoization The term "memoization" was introduced by Donald Michie in the year 1968.

Python3 Tutorial: Memoization and Decorators

It's based on the Latin word memorandum, meaning "to be remembered". It's not a misspelling of the word memorization, though in a way it has something in common. Memoisation is a technique used in computing to speed up programs. This is accomplished by memorizing the calculation results of processed input such as the results of function calls. Memoization can be explicitly programmed by the programmer, but some programming languages like Python provide mechanisms to automatically memoize functions. DecoratorLibrary. Functional Programming HOWTO — Python v2.7.5 documentation. In this document, we’ll take a tour of Python’s features suitable for implementing programs in a functional style.

Functional Programming HOWTO — Python v2.7.5 documentation

After an introduction to the concepts of functional programming, we’ll look at language features such as iterators and generators and relevant library modules such as itertools and functools. Introduction This section explains the basic concept of functional programming; if you’re just interested in learning about Python language features, skip to the next section. Charming Python: Functional programming in Python, Part 1. We'd better start with the hardest question: "What is functional programming (FP), anyway?

Charming Python: Functional programming in Python, Part 1

" One answer would be to say that FP is what you do when you program in languages like Lisp, Scheme, Haskell, ML, OCAML, Clean, Mercury, or Erlang (or a few others). That is a safe answer, but not one that clarifies very much. Unfortunately, it is hard to get a consistent opinion on just what FP is, even from functional programmers themselves. A story about elephants and blind men seems apropos here. It is also safe to contrast FP with "imperative programming" (what you do in languages like C, Pascal, C++, Java, Perl, Awk, TCL, and most others, at least for the most part).

Personally, I would roughly characterize functional programming as having at least several of the following characteristics. Python Conquers The Universe. Modified to use the WordPress [sourcecode] tag — 2012-01-14 There are a lot of tutorials[1] for Python’s lambda out there. One that I stumbled across recently and really found helpful was Mike Driscoll’s discussion of lambda on the Mouse vs Python blog. When I first started learning Python, one of the most confusing concepts to get my head around was the lambda statement.

I’m sure other new programmers get confused by it as well… Mike’s discussion is excellent: clear, straight-forward, with useful illustrative examples. Functional Programming with Python. By Alexey Kachayev, 2012 About me Position: CTO at Kitapps Inc. Production experience: Python, Java, JS, Go, Scala, Clojure, Erlang Looked at: Haskell, Lisp, Scheme Goals. Tail Recursion Elimination. I recently posted an entry in my Python History blog on the origins of Python's functional features. A side remark about not supporting tail recursion elimination (TRE) immediately sparked several comments about what a pity it is that Python doesn't do this, including links to recent blog entries by others trying to "prove" that TRE can be added to Python easily. So let me defend my position (which is that I don't want TRE in the language).

If you want a short answer, it's simply unpythonic. Here's the long answer: Advanced Python or Understanding Python. How to Speed up a Python Program 114,000 times. High Performance Python I. Code Like a Pythonista: Idiomatic Python. In this interactive tutorial, we'll cover many essential Python idioms and techniques in depth, adding immediately useful tools to your belt. There are 3 versions of this presentation: ©2006-2008, licensed under a Creative Commons Attribution/Share-Alike (BY-SA) license. My credentials: I am a resident of Montreal,father of two great kids, husband of one special woman,a full-time Python programmer,author of the Docutils project and reStructuredText,an editor of the Python Enhancement Proposals (or PEPs),an organizer of PyCon 2007, and chair of PyCon 2008,a member of the Python Software Foundation,a Director of the Foundation for the past year, and its Secretary.

In the tutorial I presented at PyCon 2006 (called Text & Data Processing), I was surprised at the reaction to some techniques I used that I had thought were common knowledge. Many of you will have seen some of these techniques and idioms before. These are the guiding principles of Python, but are open to interpretation. Python programming idioms. November 2007 Summary Here are some idioms that I regularly use when programming in Python. I've tested all of these idioms in Python 2.4. Let me know if you have any suggestions or bug reports. Strings Replacements for simple regexps. A few things to remember while coding in Python. Hidden features of Python. (How to Write a (Lisp) Interpreter (in Python))

Syntax and Semantics of the Lispy Scheme Subset The syntax of a language is what it looks like; the semantics is what it means. For example, in the language of mathematical expressions, the syntax for adding two plus two is "2 + 2" and the semantics of that expression is the number four. We say we are evaluating a syntactic expression when we determine its semantic referent; we would say that "2 + 2" evaluates to 4, and write that as "2 + 2" ⇒ 4. Learning Python Programming Language Through Video Lectures. PyCon 2010:The Mighty Dictionary (#55) Pycon 2010 atlanta presents the mighty dictionary (#55) by brandon craig rhodes video produced by carl karsten & a team in conjunction with the psf and support from: [sauce ___ ijwbitz you too can support the psf: Python - Advanced List Sorting.

Consider a list of tuples. Python: copying a list the right way. New = old[:] Those proficient in Python know what the previous line do. It copies the list old into new. This is confusing for beginners and should be avoided. How to use *args and **kwargs in Python. Operators and String Formatting in Python. Although not actually modulus, the Python % operator works similarly in string formatting to interpolate variables into a formatting string. If you've programmed in C, you'll notice that % is much like C's printf(), sprintf(), and fprintf() functions. Advanced String Formatting. PEP Index> PEP 3101 -- Advanced String Formatting Abstract. Doctest – Testing through documentation. Doctest lets you test your code by running examples embedded in the documentation and verifying that they produce the expected results. It works by parsing the help text to find examples, running them, then comparing the output text against the expected value.

Many developers find doctest easier than unittest because in its simplest form, there is no API to learn before using it. However, as the examples become more complex the lack of fixture management can make writing doctest tests more cumbersome than using unittest. Getting Started The first step to setting up doctests is to use the interactive interpreter to create examples and then copy and paste them into the docstrings in your module. Generator Tricks for Systems Programmers. Generator Tricks for Systems Programmers Copyright (C) 2008David M. Presented at PyCon'08, March 13, 2008, Chicago, Illinois. Related Tutorials Introduction. __slots__ in Python: Save some space and prevent member variable additions.