Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > question on log as an instance method

Reply
Thread Tools

question on log as an instance method

 
 
Franck Ditter
Guest
Posts: n/a
 
      10-07-2012
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
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      10-07-2012
On Sun, Oct 7, 2012 at 1:33 AM, Franck Ditter <> 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/opera...l#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/dat...-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/dat...bject.__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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-07-2012
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
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      10-07-2012
On Sun, Oct 7, 2012 at 1:33 AM, Franck Ditter <> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why can't an instance instantiated within a class method access aprotected instance method? Greg Hauptmann Ruby 9 06-16-2008 10:16 AM
instance method adding another instance method to the class Raj Singh Ruby 2 05-29-2008 10:09 PM
TypeError: unbound method PrintInput() must be called with test instance as first argument (got test instance instead) arotem Python 4 10-17-2005 07:52 AM
accessing class instance variable from instance method David Garamond Ruby 5 06-08-2004 02:26 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57