question on log as an instance method

F

Franck Ditter

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

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 ?AttributeError: 'float' object has no attribute '__log__'

Thanks for your answers.

franck
 
C

Chris Rebert

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)

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 ?
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
 
S

Steven D'Aprano

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

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 ?
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.
 
C

Chris Rebert

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top