question on log as an instance method

Discussion in 'Python' started by Franck Ditter, Oct 7, 2012.

  1. 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
     
    Franck Ditter, Oct 7, 2012
    #1
    1. Advertising

  2. Franck Ditter

    Chris Rebert Guest

    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/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
     
    Chris Rebert, Oct 7, 2012
    #2
    1. Advertising

  3. 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
     
    Steven D'Aprano, Oct 7, 2012
    #3
  4. Franck Ditter

    Chris Rebert Guest

    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
     
    Chris Rebert, Oct 7, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henrik_the_boss
    Replies:
    0
    Views:
    2,660
    Henrik_the_boss
    Nov 5, 2003
  2. Amratash
    Replies:
    0
    Views:
    530
    Amratash
    Apr 13, 2004
  3. David Garamond
    Replies:
    5
    Views:
    250
    Ara.T.Howard
    Jun 8, 2004
  4. Raj Singh
    Replies:
    2
    Views:
    200
    Rick DeNatale
    May 29, 2008
  5. Greg Hauptmann
    Replies:
    9
    Views:
    250
    Loren Segal
    Jun 16, 2008
Loading...

Share This Page