[Python-ideas] Enums

Michael Foord fuzzyman at gmail.com
Mon Jul 25 23:39:50 CEST 2011


On 25 July 2011 21:47, Robert Kern <robert.kern at gmail.com> wrote:

> On 7/25/11 1:43 PM, Michael Foord wrote:
>
>  Some apis (for example those exported directly from C) can't work with
>> something
>> that isn't a real int.
>>
>
> I think almost all of those will usually accept an object that implements
> __int__ and __index__, don't they? E.g.
>
> [~]
> |1> import os
>
> [~]
> |2> class A(object):
> ..>     def __init__(self,x):
> ..>         self.x = x
> ..>     def __int__(self):
> ..>         return self.x
> ..>     def __index__(self):
> ..>         return self.x
> ..>
>
> [~]
> |10> os.open('foo', A(os.O_RDWR))
> 21
>
>
> os.open() just uses PyArg_ParseTuple(), like most extension functions. I'm
> sure you could write an extension function that would reject A() instances,
> but it's more work, so most people don't.
>
>
Providing __index__ may indeed be enough.

Michael


>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma
>  that is made terrible by our own mad attempt to interpret it as though it
> had
>  an underlying truth."
>  -- Umberto Eco
>
> ______________________________**_________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/**mailman/listinfo/python-ideas<http://mail.python.org/mailman/listinfo/python-ideas>
>



-- 

http://www.voidspace.org.uk/

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110725/1b24c277/attachment.html>


More information about the Python-ideas mailing list