background preloader

Metapython

Facebook Twitter

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). A snake is a kind of reptile. 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 Dashed Arrow Down Rule Figure 4.2. Squasher. Combine Solid Arrows Rule snake. Python Attributes and Methods. Ahí va la virgen! Metaclases! (con Python) | CRySoL. Esta receta es una pequeña introducción a la meta-programación, concretamente voy a contar algunas cosillas sobre uno de los mecanismos más populares y potentes: las metaclases. Introducción ¿Qué es una metaclase? Pues es una clase cuyas instancias son clases en lugar de objetos. Es decir, si para construir un objeto usas una clase, para construir una clase usas una metaclase.

¿Y eso para qué sirve? Pues resulta muy útil principalmente para dos cosas: Cuando no sea posible determinar el tipo de un objeto hasta el momento de la ejecución del programa, o cuando sea necesario crear una clase a la medida de las circunstancias. Nota: Aunque es perfectamente posible (aunque doloroso) crear metaclases con “clases clásicas”, en esta receta me voy a centrar únicamente en el uso de clases del “nuevo-estilo”, aunque ya no son muy nuevas que digamos; aparecieron en Python 2.1. Requisitos Para que puedas sacarle todo el juego a esta receta necesitas: Un problema Imagina que tienes un conjunto de clases: Metaclass programming in Python, Part 3. Introduction Last year, I attended the EuroPython 2006 conference. The conference was good, the organization perfect, the talks of very high level, the people extremely nice.

Nonetheless, I noticed something of a disturbing trend in the Python community that prompted this article. Almost simultaneously, my co-author David Mertz was reflecting on a similar issue with some submitted patches to Gnosis Utilities. We have nothing against cleverness in experimental projects and learning exercises. For this article, we take a ruthless stance: we consider metaclass abuse any usage of a metaclass where you could have solved the same problem equally well without a custom metaclass.

One of the most common metaprogramming scenarios is the creation of classes with attributes and methods that are dynamically generated. Back to top About class initialization During class creation, attributes and methods of classes are set once and for all. Listing 1. Listing 2. When metaclasses become complicated.