background preloader

General, how-tos

Facebook Twitter

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.

Code Like a Pythonista: Idiomatic Python

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. Use setup.py to Deploy Your Python App with Style. Question: You've just written an awesome Python app, and you want to share it with the world.

Use setup.py to Deploy Your Python App with Style

What do you do? Answer: You use setuptools and distutils to deploy your application. Honestly deploying your Python application might be the worst documented common task in the world. Python Tips, Tricks, and Hacks. Tree Walking: Additional example. Additional example This is specifically about the claim that Python's 1000 deep recursion limit makes it impossible to walk unbalanced trees. There are simple and pythonic ways to iterate over trees, and I will illustrate one. I greatly prefer this to the typical tree recursion process.

Introduction — Python v2.7.2 documentation. The Application Programmer’s Interface to Python gives C and C++ programmers access to the Python interpreter at a variety of levels.

Introduction — Python v2.7.2 documentation

The API is equally usable from C++, but for brevity it is generally referred to as the Python/C API. There are two fundamentally different reasons for using the Python/C API. The first reason is to write extension modules for specific purposes; these are C modules that extend the Python interpreter. Python from Scratch: Object Oriented Programming. Welcome back to lesson four in our Python from Scratch series.

Python from Scratch: Object Oriented Programming

This tutorial will assume some prior knowledge of variables, data types, functions and print output. If you're not up to date, check out the previous three articles in the series to catch up. Today, we're going to be delving into the subject of Object Oriented Programming (OOP). OOP is a very powerful way of organizing your code, and a solid understanding of the concepts behind it can really help you get the most out of your coding. Prefer a Screencast? Transcription What is Object Oriented Programming? Python is primarily designed as an object-oriented programming language – but what does ‘object oriented’ actually mean? There are a variety of definitions for the term, and you could talk for literally hours trying to explain the complicated ins and outs, nuances and differences in implementations, but I’ll try to give a quick overview.

OOP puts objects at the center of the process. Getting Started. Introduction to Python: Class 5. Page Contents A Python class is created by a class definition, has an associated name space, supports attribute reference, and is callable. class name[(expr[,expr]*)]: suite The class definition is an executable statement and as such can be used whereever an executable statement may occur.

Introduction to Python: Class 5

When executed, each expr is evaluated and must evaluate to a class; then the suite is executed in a new local name space: the assumption is that the statements in the suite will make bindings in this name space, so typically the statements in the suite are assignments and function definitions. After execution of the suite, name is bound to the new class in the outer (calling) name space, and the new name space of the class definition is associated with the class object. Classes have five predefined attributes: The simplest use of classes is as simple Cartesian product types, e.g., the records of Pascal or the structs of C. class foo: a, b, c = 0, "bar", (1,2) Instantiating Classes.

Python syntax and semantics. Keywords[edit] Python has the following keywords or reserved words; they cannot be used as identifiers.[3][4] andasassertbreakclasscontinuedefdelelifelseexceptexec (changed to a built-in function in 3.x)False (keyword in 3.x)finallyforfromglobalifimportinislambdaNonenonlocal (added in 3.x)notorpassprint (changed to a built-in function in 3.x)raisereturnTrue (keyword in 3.x)trywhilewithyield As exec and print are functions as of Python 3 they are not reserved words anymore.

Python syntax and semantics

Indentation[edit] In so-called "free-format" languages, that use the block structure derived from ALGOL, blocks of code are set off with braces ({ }) or keywords. Void foo(int x){ if (x == 0) { bar(); baz(); } else { qux(x); foo(x - 1); }} Python - Object Oriented. Python has been an object-oriented language since it existed.

Python - Object Oriented

Because of this, creating and using classes and objects are downright easy. Members.gamedev.net/sicrane/articles/EmbeddingPythonPart1.html. Embedding Python with Boost.Python Part 1 by Howard "SiCrane" Jeng Python is an increasingly popular programming language used in a variety of contexts.

members.gamedev.net/sicrane/articles/EmbeddingPythonPart1.html

Python has been called a language optimized for development speed. This puts it in contrast with compiled languages like C and C++, which can be called languages optimized for execution speed. Effbot.org. Think Python.