Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #93776 > unrolled thread
| Started by | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| First post | 2015-07-14 14:45 +1000 |
| Last post | 2015-07-14 08:26 +0200 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2015-07-14 14:45 +1000 |
| Subject | Foo.__new__ is what species of method? |
| Message-ID | <mailman.478.1436849134.3674.python-list@python.org> |
Howdy all,
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.
<URL:https://docs.python.org/3/reference/datamodel.html#object.__new__>
But a “static method” is described, elsewhere in the documentation
<URL:https://docs.python.org/3/library/functions.html#staticmethod>, as
“A static method does not receive an implicit first argument”.
What the ‘__new__’ documentation describes would match a “class method”
<URL:https://docs.python.org/3/library/functions.html#classmethod>
“A class method receives the class as implicit first argument”.
I suspect this a bug in the reference documentation for ‘__new__’, and
it should instead say “__new__ is a class method …”. Am I wrong?
--
\ “Cooles & Heates: If you want just condition of warm in your |
`\ room, please control yourself.” —air conditioner instructions, |
_o__) Japan |
Ben Finney
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2015-07-14 14:54 +1000 |
| Message-ID | <55a495ee$0$1580$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #93776 |
On Tuesday 14 July 2015 14:45, Ben Finney wrote: > Howdy all, > > 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: py> class Spam(object): ... def __new__(cls): ... print cls ... py> Spam.__new__() # implicit first arg? Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: __new__() takes exactly 1 argument (0 given) py> Spam.__new__(Spam) <class '__main__.Spam'> Furthermore: py> type(Spam.__dict__['__new__']) <type 'staticmethod'> > I suspect this a bug in the reference documentation for ‘__new__’, and > it should instead say “__new__ is a class method …”. Am I wrong? I've made that mistake in the past too :-) -- Steve
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2015-07-14 15:17 +1000 |
| Message-ID | <mailman.480.1436851067.3674.python-list@python.org> |
| In reply to | #93777 |
Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes: > py> class Spam(object): > ... def __new__(cls): > ... print cls > ... > py> Spam.__new__() # implicit first arg? > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > TypeError: __new__() takes exactly 1 argument (0 given) Thanks, I'm glad I checked before filing a bug report. Hopefully I'll remember that clear test, when I need to remember which species it is :-) -- \ “Instead of having ‘answers’ on a math test, they should just | `\ call them ‘impressions’, and if you got a different | _o__) ‘impression’, so what, can't we all be brothers?” —Jack Handey | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2015-07-14 08:26 +0200 |
| Message-ID | <mailman.484.1436855214.3674.python-list@python.org> |
| In reply to | #93777 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web