background preloader

Help and Tips

Facebook Twitter

Python Is Not Java (dirtSimple.org) I was recently looking at the source of a wxPython-based GUI application, about 45.5KLOC in size, not counting the libraries used (e.g.

Python Is Not Java (dirtSimple.org)

Twisted). How to use Virtualenv in Python. Decorator Design Pattern. Intent Attach additional responsibilities to an object dynamically.

Decorator Design Pattern

Decorators provide a flexible alternative to subclassing for extending functionality.Client-specified embellishment of a core object by recursively wrapping it.Wrapping a gift, putting it in a box, and wrapping the box. Problem You want to add behavior or state to individual objects at run-time. Inheritance is not feasible because it is static and applies to an entire class. Discussion. Understanding Python Decorators in 12 Easy Steps! Ok, perhaps I jest.

Understanding Python Decorators in 12 Easy Steps!

As a Python instructor, understanding decorators is a topic I find students consistently struggle with upon first exposure. That’s because decorators are hard to understand! Getting decorators requires understanding several functional programming concepts as well as feeling comfortable with some unique features of Python’s function definition and function calling syntax. 30 Python Language Features and Tricks You May Not Know About.

1 Introduction Since I started learning Python, I decided to maintain an often visited list of "tricks".

30 Python Language Features and Tricks You May Not Know About

Any time I saw a piece of code (in an example, on Stack Overflow, in open source software, etc.) that made me think "Cool! Program Configuration in Python. Simple & Profound Python Tricks. 7 tips to Time Python scripts and control Memory & CPU usage. When running a complex Python program that takes quite a long time to execute, you might want to improve its execution time.

7 tips to Time Python scripts and control Memory & CPU usage

But how? First of all, you need the tools to detect the bottlenecks of your code, i.e. which parts take longer to execute. This way, you can concentrate in speeding these parts first. And also, you should also control the memory and CPU usage, as it can point you towards new portions of code that could be improved. Therefore, in this post I’ll comment on 7 different Python tools that give you some insight about the execution time of your functions and the Memory and CPU usage. Python IAQ: Infrequently Answered Questions. A question is infrequently answered either because few people know the answer or because it concerns an obscure, subtle point (but a point that may be crucial to you).

Python IAQ: Infrequently Answered Questions

I thought I had invented the term for my Java IAQ, but it also shows up at the very informative About.com Urban Legends site. There are lots of Python FAQs around, but this is the only Python IAQ, except for the Chinese translation of this page by Weiyang Chen, the Russian translation by Alexander Sviridenko, and the Japanese translation by Akihiro Takizawa. (There are a few Infrequently Asked Questions lists, including a satirical one on C.) What never? Well, hardly ever. DevDocs/Python. Closures In Python. You might have lived a long and happy coding life without ever needing to know what closures are.

Closures In Python

For one, many languages like C or C++ don't even support those (edit: C++ does support them via lambdas, of course. Sorry for any misinformation). Many functional languages rely heavily on closures. Others, like Python, support it, but it's not at the very core of the language (or the language philosophy, for that matter). Python, however, is a fine language for getting to know closures. Before I even attempt some kind of definition (wikipedia/closure has one, albeit somewhat complicated) or give you an example, I'll tell you what closures are about: They are about maintaining state of some kind in functions. The definitive guide on how to use static, class or abstract methods in Python. Doing code reviews is a great way to discover things that people might struggle to comprehend.

The definitive guide on how to use static, class or abstract methods in Python

While proof-reading OpenStack patches recently, I spotted that people were not using correctly the various decorators Python provides for methods. So here's my attempt at providing me a link to send them to in my next code reviews. :-) A method is a function that is stored as a class attribute. You can declare and access such a function this way: >>> class Pizza(object):... def __init__(self, size):... self.size = size... def get_size(self):... return self.size... >>> Pizza.get_size<unbound method Pizza.get_size> Python Cheat Sheet. String String Methods Array Indexes and Slices.

Python Cheat Sheet

The Python “with” Statement by Example. Advanced Python or Understanding Python. Planet Python. Documentation Index. Improving Your Python Productivity. I've been programming in python for a few years now, and I'm still often amazed by how clear and DRY well written Python code can be.

Improving Your Python Productivity

There are many small tips and tricks I've learned over time, mostly by reading the code of popular open source projects, such as Django, Flask, Requests and others. Here are a few things I've picked up that are sometimes overlooked, but can really help with everyday work. 1. Dictionary and Set comprehensions. Eventual Consistency. Python comes with a large number of built-in functions, operators and keywords. They make working with data structures and built-in types very easy, but usually when we define our own data types (classes) we also tend to come up with our own ways to manipulate and consume our data. One of the nice things in python, is that we don't have to. We can use "underscore methods" to make our classes compatible with the built-in functions and operators.

This makes our code easier to use and does a better job of hiding our nasty, complex implementation from the user. Python Iteration. Created 24 April 2012, last updated 19 March 2013 This is a presentation I gave at PyCon 2013. You can read the slides and text on this page, or open the actual presentation in your browser (use right and left arrows to advance the slides), or watch the video: A talk for PyCon 2013. This talk is billed as Beginner, and sounds like a beginner topic, but I prefer to think of it as Fundamental. Plenty of expert Python programmers aren't making enough use of the tools I'm going to talk about.

Python Community Sites

Alex Martelli Python Materials. Pypy - hacking it, using it. Speed/PerformanceTips. This page is devoted to various tips and tricks that help improve the performance of your Python programs. Wherever the information comes from someone else, I've tried to identify the source. Python has changed in some significant ways since I first wrote my "fast python" page in about 1996, which means that some of the orderings will have changed. I migrated it to the Python wiki in hopes others will help maintain it. You should always test these tips with your application and the specific version of the Python implementation you intend to use and not just blindly accept that one method is faster than another.

See the profiling section for more details. Also new since this was originally written are packages like Cython, Pyrex, Psyco, Weave, Shed Skin and PyInline, which can dramatically improve your application's performance by making it easier to push performance-critical code into C or machine language. Other Versions Russian: Overview: Optimize what needs optimizing. What are the drawbacks of Stackless Python. Python - What stackless programming languages are available. 3. Using Python on Windows — Python v2.7.2 documentation. This document aims to give an overview of Windows-specific behaviour you should know about when using Python on Microsoft Windows. 3.1. A Python for Windows Tutorial. PyCon 2010:The Mighty Dictionary (#55) Default Parameter Values in Python. 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. Hidden features of Python.