Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65391
| From | Gregory Ewing <greg.ewing@canterbury.ac.nz> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: __init__ is the initialiser |
| Date | 2014-02-04 12:47 +1300 |
| Message-ID | <blao42F4510U1@mid.individual.net> (permalink) |
| References | <lcgtpf$tui$1@ger.gmane.org> <mailman.6217.1391197950.18130.python-list@python.org> <52ec6d1f$0$29972$c3e8da3$5496439d@news.astraweb.com> <bl836qFh9baU1@mid.individual.net> <52eee3c5$0$29972$c3e8da3$5496439d@news.astraweb.com> |
Steven D'Aprano wrote:
> # --- Untested ---
> # Automatically call each __new__ constructor method, starting from
> # the most fundamental (object) and ending with the current class.
> stack = []
> for c in cls.__mro__:
> if hasattr(c, '__new__'):
> stack.append(c.__new__)
> while stack:
> stack.pop()(*args)
That sort of thing doesn't allow for passing different
arguments to the base __new__.
Also remember that in Python, __new__ isn't actually
required to call a matching base version at all -- it's
legal to do something quite different, such as returning
a cached instance or instantiating a different type of
object altogether.
> What I meant by backwards compatibility is that prior to the introduction
> of new-style classes, you couldn't override __new__, only __init__. So if
> you had a classic class, you'd have to receive the instance:
>
> class Classic:
> def __init__(self, *args):
> ...
>
> but for new-style classes, you'd receive the class:
>
> class Newstyle(object):
> def __init__(cls, *args):
> ...
What I'm saying is that even if all classes had been
new-style from the beginning, it's by no means certain that
Guido wouldn't have come up with the __new__/__init__
system anyway, because of the flexibility it provides.
Python isn't the only language to do something like that.
In Objective-C, the basic pattern for instantiating an
object goes like
[[SomeClass alloc] init: args]
where 'alloc' allocates memory for the object and
'init:' or some variation thereof initialises it.
--
Greg
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: __init__ is the initialiser Ned Batchelder <ned@nedbatchelder.com> - 2014-01-31 14:52 -0500
Re: __init__ is the initialiser Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-01 03:42 +0000
Re: __init__ is the initialiser Chris Angelico <rosuav@gmail.com> - 2014-02-01 15:35 +1100
Re: __init__ is the initialiser Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-01 05:51 +0000
Re: __init__ is the initialiser Ethan Furman <ethan@stoneleaf.us> - 2014-02-01 00:28 -0800
Re: __init__ is the initialiser Ethan Furman <ethan@stoneleaf.us> - 2014-01-31 20:55 -0800
Re: __init__ is the initialiser Ned Batchelder <ned@nedbatchelder.com> - 2014-02-01 07:28 -0500
Re: __init__ is the initialiser Roy Smith <roy@panix.com> - 2014-02-01 09:40 -0500
Re: __init__ is the initialiser Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-01 15:07 +0000
Re: __init__ is the initialiser Roy Smith <roy@panix.com> - 2014-02-01 11:17 -0500
Re: __init__ is the initialiser Tim Delaney <timothy.c.delaney@gmail.com> - 2014-02-02 07:09 +1100
Re: __init__ is the initialiser Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-02 01:28 +0000
Re: __init__ is the initialiser Ben Finney <ben+python@benfinney.id.au> - 2014-02-02 15:27 +1100
Re: __init__ is the initialiser Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-02-03 12:38 +1300
Re: __init__ is the initialiser Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-03 00:33 +0000
Re: __init__ is the initialiser Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-02-04 12:47 +1300
Re: __init__ is the initialiser Tim Delaney <timothy.c.delaney@gmail.com> - 2014-02-03 11:02 +1100
csiph-web