[Tutor] pure symbol -- __subtype__

Steven D'Aprano steve at pearwood.info
Thu Feb 18 12:08:22 CET 2010


On Thu, 18 Feb 2010 08:13:33 pm spir wrote:
> Hello,
>
> I was lately implementing a kind of "pure symbol" type. What I call
> pure symbols is these kinds of constants that refer to pure "idea",
> so that they have no real obvious value. We usually _arbitrarily_
> give them as value an int, a string, a boolean, an empty object:

If you are interested in this, there are various modules on PyPI for 
them, such as:

http://pypi.python.org/pypi/enum/

which probably does everything you want.

Unfortunately any enum solution is going to be rather heavyweight 
compared to (say) Pascal, which can simply give each enum an integer 
value and have the compiler enforce separation of types. (In other 
words, all the heavy complexity is in the compiler rather than the 
runtime environment.)

If you want a lightweight solution, just use constant strings or 
integers.


> Actually, what I need is a kind of __subtype__ magic method that acts
> for subtyping the same way __init__ does for instanciation. 

That's what metaclasses are for.



-- 
Steven D'Aprano


More information about the Tutor mailing list