background preloader

Idioms

Facebook Twitter

Momentary Fascinations: Bound Inner Classes For Python. (This blog entry is my contribution to the 2013 Python Advent Calendar.

Momentary Fascinations: Bound Inner Classes For Python

I'm the entry for December 25th,; however, due to the time zone difference between here and Japan, I'm posting it during what is to me the morning of the 24th. Merry Christmas!) In Python, something magic happens when you put a function inside a class. If you access that function through an instance of the class, you don't simply get the function back. Instead you get a new object we call a "method". But this magic is only true for functions.

Inner classes are rarely used in Python. I often use inner classes in Python anyway, because I have classes that conceptually should live inside some other class. Class D: class C: def __init__(self, outer): self.outer = outer d = D() c = d.C(d) Wouldn't it be nice if inner classes got "bound" the way functions did? But is this even possible in Python? I was amazed at the replies. With Alex's permission, I posted this as a "recipe" at the Python Cookbook: p.s. Advanced Design Patterns in Python. The aim of this tutorial is to show off Advanced design structures in Python and the best way to use them.

Advanced Design Patterns in Python

Depending on what you need from a data structure, whether it’s fast lookup, immutability, indexing, etc, you can choose the best data structure for the job and most of the time, you will be combining data structures together to get a logical and easy to understand data model. Python data structures are very intuitive from a syntax point of view and they offer a large choice of operations. This tutorial tries to put together the most common and useful information about each data structure and offer a guide on when it is best to use one structure or another. Comprehensions If you’ve used Python for very long, you’ve at least heard of list comprehensions. A list comprehension consists of the following parts: Say we need to get a list of all the integers, whose value is above zero in a sequence and then square them: Pretty simple, right?

Generators to rescue this time. Decorators Patterns. The Python yield keyword explained. Hi there folks.

The Python yield keyword explained

Again welcome to yet another useful tutorial. This is again a stackoverflow answer. This one is related to the Python yield keyword. It explains you what yield, generators and iterables are. So without wasting any time lets continue with the answer. To understand what yield does, you must understand what generators are. Iterables When you create a list, you can read its items one by one, and it’s called iteration: >>> mylist = [1, 2, 3] >>> for i in mylist: ... print(i) 1 2 3 Mylist is an iterable. >>> mylist = [x*x for x in range(3)] >>> for i in mylist: ... print(i) 0 1 4 Everything you can use “for… in…” on is an iterable: lists, strings, files… These iterables are handy because you can read them as much as you wish, but you store all the values in memory and it’s not always what you want when you have a lot of values.

Generators Generators are iterators, but you can only iterate over them once. Python Yields are Fun! While you can optimize the heck out of your Python code with generators and generator expressions I'm more interested in goofing around and solving classic programming questions with the yield statement. note: For this article, since it's easier to explain things as they happen, I'll be including a lot of inline comments.

Python Yields are Fun!

Let's start with a simple function that returns a sequence of some of my favorite values: # yielding.pydef pydanny_selected_numbers(): # If you multiple 9 by any other number you can easily play with # numbers to get back to 9. # Ex: 2 * 9 = 18. 1 + 8 = 9 # Ex: 15 * 9 = 135. 1 + 3 + 5 = 9 # See yield 9 # A pretty prime. yield 31 # What's 6 * 7? Yield 42 # The string representation of my first date with Audrey Roy yield "2010/02/20" Dependency Injection The Python Way. Inversion of Control (IoC) Containers and the Dependency Injection pattern have drawn some attention in the Java world, and they are increasingly spreading over to .NET, too.

Dependency Injection The Python Way

(Perhaps we are facing a sort of "Infection OUT of Control" - IooC? ;) IoC is all about loose coupling between components of an application, about cutting off explicit, direct dependencies, plus some goodies (most of which are useful in statically typed languages only, like automatic type/interface matching). A thorough discussion on the subject can be found at . In statically typed languages, an IoC container is quite a challenge. Components do not know each other directlyComponents specify external dependencies using some sort of a key.Dependencies are resolved late, preferably just before they are used (JIT dependency resolution).Dependencies are resolved once for each component.

Decorators