last day (14 days later) » 

1:31 AM
-2
A: What is the design reason for the fact that if __new__ does not return an instance of cls, python does not invoke __init__?

Malik BrahimiThis basically differentiates between instance methods and static methods. # __new__ returns instance of cls -> __init__ is an instance method # __new__ doesn't returns instance of cls -> __init__ is an static method The difference between an instance method and static method is like so: # My...

 
this doesn't make any sense
 
Method naming is a developer's preference, let me edit in an example.
 
now your example code never calls __new__, and you don't have your own __new__ that will return something other than an Example instance, and that code would still work even if you did instantiate an object, AND that's an old-style class where __new__ doesn't exist anyway. so i still don't see what you're trying to say
 
__new__ is called implicitly as it is inherited from object
 
if you never create an instance, __new__ is never called. your answer doesn't explain the question at all.
 
1:31 AM
That's exactly why I'm differentiating between static and instance. If __init__ is static __new__ isn't called, and thus it's a traditional static method. Otherwise, it's an initializer and __new__ is called.
 
you are completely and utterly wrong. __new__ isn't called only because you never created an instance. whether __init__ is static or not has nothing to do with it. try calling Example() in your first example, and you'll see that __init__ still gets called. (also, like i said, those are old-style classes, so they don't inherit from object, and they don't have a __new__.)
 
Yes, excuse me. I copied and pasted the previous example and forgot to change that. Otherwise, I am completely and utterly correct. When Example() is called, __new__() and __init__() are called and an object is created. When Example.__init__() is called as a static method, only __init__ is called because an object isn't created.
 
this has nothing to do with whether __new__ returns an instance of the class
 
Look at the new answer just posted. It confirms my argument.
 
your argument has nothing to do with __new__? your two cases are "use the default implementation" and "don't call it at all"
the question was about what happens when __new__ returns different things
 
1:35 AM
Basically, I am saying if __new__ returns an instance of cls the __init__ is a constructor. Otherwise, __init__ is a static method.
Eevee?
 
and you are wrong
 
How so?
 
you can have a __new__ that doesn't return instances sometimes, and an __init__ that's not a staticmethod
i don't understand why you think staticmethods have anything to do with this at all
 
Yeah that is if __init__ and __new__ are both user defined.
 
...the default __new__ always returns an instance of the class
class Foo(object):
@staticmethod
def __init__():
print "hey sup"

Foo()
ok well chat doesn't support indented code blocks apparently but if you try that you will get the message printed
 
1:44 AM
and __new__ would return None in this case because __init__ isn't constructor
 
uh, no, you would also get an object
try the damn code
>>> Foo()
hey sup
<__main__.Foo object at 0x7fbe711ea250>
__init__ is called, printing the message, and an object is returned, as created by object.__new__
staticmethod has zero impact whatsoever on the instance construction protocol; it only affects calling
 
You're getting really worked up about this. You shouldn't try so hard; you're fighting against a kid.
 
you're spreading misinformation about an already confusing and nuanced topic that i have a deep interest in
 
Okay, I'll delete my post so you can sleep restfully tonight.
 
thanks
 
1:49 AM
Well, see you around
 
if you're a "kid" then maybe assume other people know what they're talking about? you can't play it both ways jeez
 
Well, at least I learned something through this discussion
You're actually pretty entertaining. I thank you for the time well spent.
 
ok, well, good day
 

last day (14 days later) »