Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65165
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: __init__ is the initialiser |
| Date | 2014-01-31 21:28 -0500 |
| References | <lcgtpf$tui$1@ger.gmane.org> <lcgut2$b8r$1@ger.gmane.org> <52EC0A86.7060102@stoneleaf.us> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.6243.1391221704.18130.python-list@python.org> (permalink) |
On 1/31/2014 3:41 PM, Ethan Furman wrote: > On 01/31/2014 11:52 AM, 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? Construct a house: Clear and grade house site. Bring in power and water. Built temporary structures. Built foundation. Built house on foundation. For most classes, __new__ stops with the foundation -- the bare object object (with the class name slapped onto it). A *house* is not constructed until the house is constructed on top of the foundation. >> 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__. Right: __init__ usually does the stuff that makes the object an instance of a particular class rather than just a bare bones object. In English, constructing a 'something' includes constructing the features that make the object a 'something' rather than something else. >>> Most classes have __init__, only very very few have __new__. *Every* class has .__new__. Mutable builtins and almost all user classes inherit .__new__ from object. Every class also has .__init__, but it is mainly inherited from object by immutable builtins. >>> tuple.__init__ <slot wrapper '__init__' of 'object' objects> User classes lacking .__init__ usually inherit it from something other than object. So objects are constructed by first calling .__new__ and then passing the result to .__init__. The Python 3 doc should say so. > My problem is with the first sentence, as it makes it sound like there > is no __new__ and everything you need is in __init__. .__new__ allocates a bare object and initializes immutable builtins. Figuring out how > __new__ and __init__ were tied together took a long time because of that. > >> Why can't we call __init__ the constructor and __new__ the allocator? -- Terry Jan Reedy
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: __init__ is the initialiser Terry Reedy <tjreedy@udel.edu> - 2014-01-31 21:28 -0500
csiph-web