background preloader

Fabelier

Facebook Twitter

Pythonista stuff - Fabelier. Be Pythonic. This article is intended for new users of Python. When going from one language to another, some things have to be unlearned (see Transfer of Learning). What you know from other languages may not be always useful in Python. This page contains some idioms used in Python that I particularly like, and I hope others find useful in their quest for Pythonicity. Wrong: i = 0 while i<10: do_something(i) i += 1 Pythonic: for i in xrange(10): do_something(i) The following example indexes a list. i = 0 while i<len(lst): do_something(lst[i]) i += 1 for item in lst: do_something(item) An iterator variable is useful when you want to maintain looping state between two 'runs': itr_lst = iter(lst) for item in itr_lst: do_something(item) if is_some_condition(item): break for item in itr_lst: # continues where previous loop left off do_something_else(item) Python provides many higher level facilities to operate on sequences, such as zip(), max(), min(), list comprehensions, generator expressions and so on.

Iterator - The Python yield keyword explained. Zen of python. Python Types and Objects. Can Skim Section This oddly placed section explains the type-instance and supertype-subtype relationships, and can be safely skipped if the reader is already familiar with these OO concepts. Skimming over the rules below might be useful though. While we introduce many different objects, we only use two kinds of relationships (Figure 4.1, “Relationships”): is a kind of (solid line): Known to the OO folks as specialization, this relationship exists between two objects when one (the subclass) is a specialized version of the other (the superclass). Note that in plain English, the term 'is a' is used for both of the above relationships.

Figure 4.1. We use the solid line for the first relationship because these objects are closer to each other than ones related by the second. It is useful at this point to note the following (independent) properties of relationships: Dashed Arrow Up Rule If X is an instance of A, and A is a subclass of B, then X is an instance of B as well. Dashed Arrow Down Rule.

Oop - What is a metaclass 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. 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. Import this. Beginning Test-Driven Development in Python. Test-driven development (TDD) is a process that has been documented considerably over recent years. A process of baking your tests right into your everyday coding, as opposed to a nagging afterthought, should be something that developers seek to make the norm, rather than some ideal fantasy. I will introduce the core concepts of TDD. The whole process is very simple to get to grips with, and it shouldn't take too long before you wonder how you were able to get anything done before! There are huge gains to be made from TDD - namely, the quality of your code improving, but also clarity and focus on what it is that you are trying to achieve, and the way in which you will achieve it.

TDD also works seamlessly with agile development, and can best be utilized when pair-programming, as you will see later on. In this tutorial, I will introduce the core concepts of TDD, and will provide examples in Python, using the nosetests unit-testing package. What is Test-Driven Development? Success! Or: How can I make a chain of function decorators in Python. Python progression path - From apprentice to guru. Les articles pour apprendre Python, dans le bon ordre :-) Ceci n’est pas un cours complet et cohérent sur “apprendre à programmer avec Python”. Pour ça voyez plutôt le site du zéro ou le livre gratuit de Swinnen. Ceci est une compilation de tous les cours et tutos du blog, filtrés, et ordonnés de manière cohérente. C’est bordélique, y a des trous partout, mais il y a aussi des connaissances que vous ne trouverez nulle part ailleurs aussi bien expliquées qu’ici. Ce qu’il faut savoir Aller plus loin Les outils autour de Python Cookbook Les exercices Passage à Python 3 Culture générale autour de Python.

Hidden features of Python. Python Attributes and Methods. Fabelier | a * Lab to make things.