Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #93788
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Subject | Re: Foo.__new__ is what species of method? |
| Date | 2015-07-14 08:26 +0200 |
| References | <mailman.478.1436849134.3674.python-list@python.org> <55a495ee$0$1580$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.484.1436855214.3674.python-list@python.org> (permalink) |
Steven D'Aprano schrieb am 14.07.2015 um 06:54:
> On Tuesday 14 July 2015 14:45, Ben Finney wrote:
>> The Python reference says of a class ‘__new__’ method::
>>
>> object.__new__(cls[, ...])
>>
>> Called to create a new instance of class cls. __new__() is a static
>> method (special-cased so you need not declare it as such) that takes
>> the class of which an instance was requested as its first argument.
>
> This is correct. __new__ is a static method and you need to explicitly
> provide the cls argument:
And it needs to be that way in order to allow superclass calls in a
subclass's __new__ method:
class Super(object):
def __new__(cls):
return object.__new__(cls)
class Sub(Super):
def __new__(cls):
return Super.__new__(cls)
If it was a classmethod, it would receive the class you call it on as first
argument (i.e. "Super" and "object" above), not the class you want to
instantiate (i.e. "Sub" or "Super").
Stefan
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Foo.__new__ is what species of method? Ben Finney <ben+python@benfinney.id.au> - 2015-07-14 14:45 +1000
Re: Foo.__new__ is what species of method? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-07-14 14:54 +1000
Re: Foo.__new__ is what species of method? Ben Finney <ben+python@benfinney.id.au> - 2015-07-14 15:17 +1000
Re: Foo.__new__ is what species of method? Stefan Behnel <stefan_ml@behnel.de> - 2015-07-14 08:26 +0200
csiph-web