[Python-Dev] Semantics of __int__(), __index__()

Nick Coghlan ncoghlan at gmail.com
Fri Apr 5 01:56:09 CEST 2013


On 5 Apr 2013 01:07, "Chris Angelico" <rosuav at gmail.com> wrote:
>
> On Fri, Apr 5, 2013 at 1:59 AM, Guido van Rossum <guido at python.org> wrote:
> > On Thu, Apr 4, 2013 at 7:47 AM, Chris Angelico <rosuav at gmail.com> wrote:
> >> Is there any argument that I can pass to Foo() to get back a Bar()?
> >> Would anyone expect there to be one? Sure, I could override __new__ to
> >> do stupid things, but in terms of logical expectations, I'd expect
> >> that Foo(x) will return a Foo object, not a Bar object. Why should int
> >> be any different? What have I missed here?
> >
> >
> > A class can define a __new__ method that returns a different object.
E.g.
> > (python 3):
> >
>
> Right, I'm aware it's possible. But who would expect it of a class?

Python 3.3 does it for OSError to map errno values to the appropriate
subclasses. That's mainly to aid migration to the new exception structure,
though (see PEP 3151). For a clean slate API design you would use a
separate factory function or class method to do the conversion.

Cheers,
Nick.

>
> ChrisA
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
http://mail.python.org/mailman/options/python-dev/ncoghlan%40gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130405/c172858a/attachment.html>


More information about the Python-Dev mailing list