(original) (raw)
On Thu, Apr 4, 2013 at 7:47 AM, Chris Angelico <rosuav@gmail.com> wrote:
A class can define a \_\_new\_\_ method that returns a different object. E.g. (python 3):On Fri, Apr 5, 2013 at 1:23 AM, Oscar BenjaminThere's something I'm fundamentally not understanding about this
<oscar.j.benjamin@gmail.com> wrote:
\> The reason for calling int(obj) is to get an object that is precisely
\> of type int. When I call this I do not want any modified or additional
\> methods or data attached to the resulting object.
debate, and that is: How is it that calling a class can logically
return anything other than an instance of that class? Taking it to a
user-defined type:
class Foo:
� � pass
class Bar(Foo):
� � pass
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?
�
>>> class C:
...�� def \_\_new\_\_(cls): return 42
...
>>> C()
42
>>>