Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65138 > unrolled thread

Re: __init__ is the initialiser

Started byMark Lawrence <breamoreboy@yahoo.co.uk>
First post2014-01-31 20:17 +0000
Last post2014-01-31 20:17 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: __init__ is the initialiser Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-31 20:17 +0000

#65138 — Re: __init__ is the initialiser

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-01-31 20:17 +0000
SubjectRe: __init__ is the initialiser
Message-ID<mailman.6218.1391199439.18130.python-list@python.org>
On 31/01/2014 19:52, Ned Batchelder wrote:
> On 1/31/14 2:33 PM, Mark Lawrence wrote:
>>  From http://docs.python.org/3/reference/datamodel.html#object.__init__
>> which states:-
>>
>> "
>> Called when the instance is created. The arguments are those passed to
>> the class constructor expression. If a base class has an __init__()
>> method, the derived class’s __init__() method, if any, must explicitly
>> call it to ensure proper initialization of the base class part of the
>> instance; for example: BaseClass.__init__(self, [args...]). As a special
>> constraint on constructors, no value may be returned; doing so will
>> cause a TypeError to be raised at runtime.
>> "
>>
>> Should the wording of the above be changed to clearly reflect that we
>> have an initialiser here and that __new__ is the constructor?
>>
>
> I'm torn about that.  The fact is, that for 95% of the reasons you want
> to say "constructor", the thing you're describing is __init__.  Most
> classes have __init__, only very very few have __new__.
>
> The sense that __new__ is the constructor is the one borrowed from C++
> and Java: you don't have an instance of your type until the constructor
> has returned.  This is why __init__ is not a constructor: the self
> passed into __init__ is already an object of your class.
>
> But that distinction isn't useful in most programs.  The thing most
> people mean by "constructor" is "the method that gets invoked right at
> the beginning of the object's lifetime, where you can add code to
> initialize it properly."  That describes __init__.
>
> Insisting that __init__ is not a constructor makes about as much sense
> as insisting that "Python has no variables" just because they work
> differently than in C.  Python has variables, and it has constructors.
> We don't have to be tied to C++ semantics of the word "constructor" any
> more than we have to tied to its semantics of the word "variable" or "for".
>
> Why can't we call __init__ the constructor and __new__ the allocator?
>

To clarify the situation I think we should call __new__ "the thing that 
instanciates an instance of a class" and __init__ "the thing that isn't 
actually needed as you can add attributes to an instance anywhere in 
your code" :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web