Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

question on log as an instance method

13 views
Skip to first unread message

Franck Ditter

unread,
Oct 7, 2012, 4:33:36 AM10/7/12
to
Hi ! Here is Python 3.2.3, MacOSX-Lion

Question : I may consider + as an hidden instance method , as
1+2 is equivalent to (1).__add__(2) ?
I also consider __abs__ as an instance method :
>>> (-2).__abs__()
2

Question 1 : could the parser cope with the mandatory space
in 1 .__add__(2) ?

Question 2 : After importing math, why can't I consider log as
an instance method, after all ?
>>> (4).__log__()
AttributeError: 'float' object has no attribute '__log__'

Thanks for your answers.

franck

Chris Rebert

unread,
Oct 7, 2012, 5:47:05 AM10/7/12
to Franck Ditter, pytho...@python.org
On Sun, Oct 7, 2012 at 1:33 AM, Franck Ditter <fra...@ditter.org> wrote:
> Hi ! Here is Python 3.2.3, MacOSX-Lion
>
> Question 0 : I may consider + as an hidden instance method , as
> 1+2 is equivalent to (1).__add__(2) ?

No, it's not nearly that simple. It's technically equivalent to
operator.add(1, 2) [
http://docs.python.org/library/operator.html#operator.add ], which
hints that there's additional logic involved. Some examples of the
complexities (in the general case):
* special methods are looked up on the objects' classes, ignoring
per-instance attributes; see
http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes
* trying to add objects of incompatible types raises TypeError rather
than AttributeError (which one might otherwise expect when a class
doesn't define __add__() [or similar])
* such TypeErrors are often raised directly by the interpreter itself,
rather than from within the body of some specific implementation of an
operator special method
* falling back to reflected methods (in the case of +, __radd__() [
http://docs.python.org/reference/datamodel.html#object.__radd__ ])
when the normal method fails or is not defined
* returning NotImplemented triggers fallback (or if already attempting
fallback, may cause the operation to fail)

<snip>
> Question 2 : After importing math,

Why would that be relevant? Python is not generally the sort of
language that would have the mere importation of a std lib module
significantly affect language semantics.

> why can't I consider log as
> an instance method, after all ?
>>>> (4).__log__()
> AttributeError: 'float' object has no attribute '__log__'

Because Python just simply did not choose to make "take the (natural)
logarithm of" a built-in, overloadable operation (hence, in part, why
you had to `import math` to even be able to access that calculation).
And it further didn't happen to define math.log() in terms of a .log()
or .__log__() instance method.

Basically, it's somewhat arbitrary and some historical reasons are involved.

Cheers,
Chris

Steven D'Aprano

unread,
Oct 7, 2012, 6:04:15 AM10/7/12
to
On Sun, 07 Oct 2012 10:33:36 +0200, Franck Ditter wrote:


> Question : I may consider + as an hidden instance method , as 1+2 is
> equivalent to (1).__add__(2) ? I also consider __abs__ as an instance
> method :
>>>> (-2).__abs__()
> 2

The short answer is, yes.

The *correct* answer is, not quite.

So-called "dunder" methods (Double leading and trailing UNDERscore)
methods like __add__, __abs__, __len__ and many others are treated
slightly differently from ordinary instance methods, but only when they
are automatically invoked by Python.

If you explicitly call `instance.__add__(value)`, __add__ is treated as
an ordinary instance method. But when you call `instance + value`, Python
automatically invokes the __add__ method, but using slightly different
method resolution rules. This is done for the sake of speed.


> Question 1 : could the parser cope with the mandatory space in 1
> .__add__(2) ?

Why not try it and see?

py> 1 .__add__(2)
3


> Question 2 : After importing math, why can't I consider log as an
> instance method, after all ?
>>>> (4).__log__()
> AttributeError: 'float' object has no attribute '__log__'


Because importing a module does not magically add new methods to classes.

Floats do not have a __log__ method, because they don't need one.
Importing math doesn't create such a method. Why would it? What is the
purpose of __log__? math.log doesn't need it.


--
Steven

Chris Rebert

unread,
Oct 7, 2012, 6:25:31 AM10/7/12
to pytho...@python.org
On Sun, Oct 7, 2012 at 1:33 AM, Franck Ditter <fra...@ditter.org> wrote:
>

As a matter of netiquette, please don't post from a
plausible-but-invalid email address, especially at a domain that
doesn't seem to belong to you. (I got a mailer-daemon bounce when
replying to your posts.)
If you must use an invalid address, then please make use of the
".invalid" TLD (that's what it's for!).

Regards,
Chris
0 new messages