You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"Class instances
Class instances are described below. Class instances are callable
only when the class has a __call__() method; x(arguments) is a shorthand
for x.__call__(arguments)."
The following program demonstrates otherwise regarding that last statement.
defcall(self):
print"inserted __call__ in object of class A"classA(object):
def__call__(self):
print"__call__ method in class A"x=A() # Equates: x = type(A).__call__(A)x.__call__=callx() # Calls the method of class A.x.__call__(x) # Calls function "call".type(x).__call__(x) # The correct longhand of x() IMHO
If I were to rephrase the documentation:
"Class instances
Class instances are described below. Class instances are callable
only when the class has a __call__() method; x(arguments) is a shorthand
for type(x).__call__(x, arguments)."
FYI, all special methods are (now) looked up on the type for new style
classes. Your suggested rewrite makes things more confusing, IMO
(partly because to make it accurate it would need to be something like
type(x).__call__(x, *args, **kw), which doesn't really make the sentence
clearer).
So far I haven't thought of a rewording I like. The best I've come up
with is "x(arguments) invokes the __call__ method, passing it the
arguments." This leaves it to other parts of the language spec to
explain how __call__ gets resolved.
Whatever we decide to do, section 3.4.4 will need a similar update.
For some reason the 3.2 docs don't contain the sentence you reference,
but they do have the same mistake in section 3.4.4. Which is even more
of a mistake in 3.x, since there are only new style classes there.
Activity
onlyme commentedon Aug 22, 2009
From:
http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy
"Class instances
Class instances are described below. Class instances are callable
only when the class has a __call__() method; x(arguments) is a shorthand
for x.__call__(arguments)."
The following program demonstrates otherwise regarding that last statement.
If I were to rephrase the documentation:
"Class instances
Class instances are described below. Class instances are callable
only when the class has a __call__() method; x(arguments) is a shorthand
for type(x).__call__(x, arguments)."
onlyme commentedon Aug 24, 2009
On further reading it seems my objections only apply to new style classes.
bitdancer commentedon Aug 24, 2009
FYI, all special methods are (now) looked up on the type for new style
classes. Your suggested rewrite makes things more confusing, IMO
(partly because to make it accurate it would need to be something like
type(x).__call__(x, *args, **kw), which doesn't really make the sentence
clearer).
So far I haven't thought of a rewording I like. The best I've come up
with is "x(arguments) invokes the __call__ method, passing it the
arguments." This leaves it to other parts of the language spec to
explain how __call__ gets resolved.
Whatever we decide to do, section 3.4.4 will need a similar update.
bitdancer commentedon Aug 24, 2009
For some reason the 3.2 docs don't contain the sentence you reference,
but they do have the same mistake in section 3.4.4. Which is even more
of a mistake in 3.x, since there are only new style classes there.
andresdelfino commentedon Jun 28, 2018
The PR uses a slight rewording of David's phrasing.
vstinner commentedon Oct 27, 2020
New changeset 95f710c by Andre Delfino in branch 'master':
bpo-6761: Enhance __call__ documentation (GH-7987)
95f710c
miss-islington commentedon Oct 27, 2020
New changeset b1ce044 by Miss Skeleton (bot) in branch '3.8':
bpo-6761: Enhance __call__ documentation (GH-7987)
b1ce044
miss-islington commentedon Oct 27, 2020
New changeset 2cb259f by Miss Skeleton (bot) in branch '3.9':
bpo-6761: Enhance __call__ documentation (GH-7987)
2cb259f