<br><br><div class="gmail_quote">On 25 July 2011 21:47, Robert Kern <span dir="ltr"><<a href="mailto:robert.kern@gmail.com">robert.kern@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On 7/25/11 1:43 PM, Michael Foord wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Some apis (for example those exported directly from C) can't work with something<br>
that isn't a real int.<br>
</blockquote>
<br></div>
I think almost all of those will usually accept an object that implements __int__ and __index__, don't they? E.g.<br>
<br>
[~]<br>
|1> import os<br>
<br>
[~]<br>
|2> class A(object):<br>
..>     def __init__(self,x):<br>
..>         self.x = x<br>
..>     def __int__(self):<br>
..>         return self.x<br>
..>     def __index__(self):<br>
..>         return self.x<br>
..><br>
<br>
[~]<br>
|10> os.open('foo', A(os.O_RDWR))<br>
21<br>
<br>
<br>
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.<div class="im">
<br></div></blockquote><div><br>Providing __index__ may indeed be enough. <br><br>Michael<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">
<br>
-- <br>
Robert Kern<br>
<br>
"I have come to believe that the whole world is an enigma, a harmless enigma<br>
 that is made terrible by our own mad attempt to interpret it as though it had<br>
 an underlying truth."<br>
  -- Umberto Eco<br>
<br>
______________________________<u></u>_________________<br></div><div><div></div><div class="h5">
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/<u></u>mailman/listinfo/python-ideas</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><pre cols="72"><a href="http://www.voidspace.org.uk/" target="_blank">http://www.voidspace.org.uk/</a><br><br>May you do good and not evil<br>May you find forgiveness for yourself and forgive others<br>
May you share freely, never taking more than you give.<br>-- the sqlite blessing <a href="http://www.sqlite.org/different.html" target="_blank">http://www.sqlite.org/different.html</a></pre>
<br>