magic names in python

Lenard Lindstrom len-l at telus.net
Mon Jun 4 18:19:35 EDT 2007


per9000 wrote:
> Hi,
> 
> I recently started working a lot more in python than I have done in
> the past. And I discovered something that totally removed the pretty
> pink clouds of beautifulness that had surrounded my previous python
> experiences: magic names (I felt almost as sad as when I discovered
> the strange pink worms that eat you in nethack, not to mention the
> mind flayers - I really hate them).
> 
> I guess all programming languages have magic names to some extent
> (f.x. classes in the "C-family" have constructors that must have the
> same name as the class (foo::foo) instead of foo.__init__).
> 
> I just used a search engine a little on this topic and I found no
> comprehensive list of magic names in python.
> 
> So my questions:
>  * is there a comprehensive list of magic names in python (so far i
> know of __init__ and __repr__)?
>  * are these lists complete or can magic names be added over time (to
> the python "core")?
>  * are magic names the same in different python versions?
> 
> I also tried (selected parts of(?)) the unittest package for use in
> Zope and it seemed functions that I created for my test with the magic
> prefix "test" were magic, other functions were not.
> 
> So another question emerges:
>  * is the use of magic names encouraged and/or part of good coding
> practice.
> 

What is "magic" about __init__ and __repr__? They are identifiers just 
like "foo" or "JustAnotherClass". They have no special meaning to the 
Python compiler. The leading and trailing double underscores represent 
no special incantation. It is just a naming convention.

So a number of method names like __init__ and __repr__ have a 
pre-defined usage. In every other respect they are just normal methods. 
They can be called directly. When dir() is called on a class they show 
up along with other method names. They can appear in a class declaration 
or added later after the class is created, just like other methods. They 
differ in that sometimes they will be called implicitly, such as when 
creating a new object or by the print statement.

As to whether to use them: absolutely. __init__ is the preferred way to 
initialize a new object. Providing a __repr__ or __str__ method lets 
objects display descriptive and meaningful information about themselves 
in print statements and exception messages. And various other "special 
methods" allow for user defined versions of Python's built-in types. In 
fact most built-in types can be extended by subclassing and overriding 
their special methods.

Back to "magic names". Python is a consistent language. What may be 
special cased - "magic" - in another language is handled normally with 
Python's general purpose dynamic type system. In fact I can think of 
only one name I would call "magic": __future__.

from __future__ import <feature>

does have special meaning to the Python compiler. It does more than 
import a module.

--
Lenard Lindstrom
<len-l at telus.net>



More information about the Python-list mailing list