double underscore attributes?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bobueland@yahoo.com

    #1

    double underscore attributes?

    Entering[color=blue][color=green][color=darkred]
    >>>dir(5)[/color][/color][/color]

    I get
    ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
    '__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
    '__floordiv__', '__getattribute __', '__getnewargs__ ', '__hash__',
    '__hex__', '__init__', '__int__', '__invert__', '__long__',
    '__lshift__', '__mod__', '__mul__', '__neg__', '__new__',
    '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__',
    '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__' ,
    '__repr__', '__rfloordiv__' , '__rlshift__', '__rmod__', '__rmul__',
    '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__',
    '__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__',
    '__truediv__', '__xor__']

    Every time I use dir(some module) I get a lot of attributes with double
    underscore, for example __add__. Ok, I thought __add__ must be a method
    which I can apply like this[color=blue][color=green][color=darkred]
    >>> 5.__add(8)[/color][/color][/color]

    However Python responded
    SyntaxError: invalid syntax

    I tried[color=blue][color=green][color=darkred]
    >>> help(5.__add__)[/color][/color][/color]

    but got
    SyntaxError: invalid syntax

    However when I tried with a list[color=blue][color=green][color=darkred]
    >>> help([5,6].__add__)[/color][/color][/color]

    I got
    Help on method-wrapper object:

    __add__ = class method-wrapper(object)
    | Methods defined here:
    |
    | __call__(...)
    | x.__call__(...) <==> x(...)
    |
    | __getattribute_ _(...)
    | x.__getattribut e__('name') <==> x.name

    Not that I understand much of this but at least I got some response.

    Now I went to Python Library Reference and searched for "__add__" but
    got zero hits.

    Could someone explain the use of __add__ (and similar double underscore
    attributes) and what their use is.

    Bob

  • Wojciech Mula

    #2
    Re: double underscore attributes?

    bobueland@yahoo .com wrote:[color=blue]
    > Now I went to Python Library Reference and searched for "__add__" but
    > got zero hits.[/color]

    The official home of the Python Programming Language

    Comment

    • LocaWapp

      #3
      Re: double underscore attributes?

      Hi Bob,
      see this my example:
      [color=blue][color=green][color=darkred]
      >>> class A:[/color][/color][/color]
      .... def __add__(a,b):
      .... print a,b
      ....[color=blue][color=green][color=darkred]
      >>> a = A()
      >>> b = A()
      >>> a + b[/color][/color][/color]
      <__main__.A instance at 0x80c5a74> <__main__.A instance at 0x80c6234>[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Comment

      • Steven Bethard

        #4
        Re: double underscore attributes?

        bobueland@yahoo .com wrote:[color=blue]
        > Every time I use dir(some module) I get a lot of attributes with double
        > underscore, for example __add__. Ok, I thought __add__ must be a method
        > which I can apply like this
        >[color=green][color=darkred]
        >>>>5.__add(8 )[/color][/color]
        >
        > However Python responded
        > SyntaxError: invalid syntax
        >
        > I tried
        >[color=green][color=darkred]
        >>>>help(5.__ad d__)[/color][/color]
        >
        > but got
        > SyntaxError: invalid syntax[/color]

        Note that these SyntaxErrors are due to how Python parses floats:
        [color=blue][color=green][color=darkred]
        >>> 5.[/color][/color][/color]
        5.0[color=blue][color=green][color=darkred]
        >>> 5.__add__(8)[/color][/color][/color]
        Traceback ( File "<interacti ve input>", line 1
        5.__add__(8)
        ^
        SyntaxError: invalid syntax[color=blue][color=green][color=darkred]
        >>> (5).__add__(8)[/color][/color][/color]
        13

        HTH,

        STeVe

        Comment

        • Dan Bishop

          #5
          Re: double underscore attributes?

          bobueland@yahoo .com wrote:
          ....[color=blue]
          > Every time I use dir(some module) I get a lot of attributes with double
          > underscore, for example __add__. Ok, I thought __add__ must be a method
          > which I can apply like this[/color]
          ....[color=blue]
          > I tried[color=green][color=darkred]
          > >>> help(5.__add__)[/color][/color]
          >
          > but got
          > SyntaxError: invalid syntax[/color]

          That's because the parser thinks "5." is a float, rather than the
          integer 5 with a dot after it. If you want to refer to an attribute of
          an integer literal, you can use "(5).__add_ _" or "5 .__add__" (with a
          space).

          Comment

          • Xavier Morel

            #6
            Re: double underscore attributes?

            bobueland@yahoo .com wrote:[color=blue]
            > Could someone explain the use of __add__ (and similar double underscore
            > attributes) and what their use is.
            >
            > Bob
            >[/color]
            Methods with double leading and trailing underscores are basically
            "magic methods" with specific meanings for the Python interpreter.

            You're not supposed to use them directly (in most cases) as they are
            wrapped by syntactic sugar or part of protocol's implementation.

            __add__, for example, is the definition of the "+" operator, using
            "(5).__add__(8) " is exactly the same as using "5+8".

            To get a list of most of these magic methods, check the Python
            documentation on the "operator" module.

            These magic methods allow you to emulate numbers, sequences, iterators,
            or even callables (allowing you to use an object as a function). Be
            really careful with them though, one of the things that plagued (and
            still plague) C++ is the abuse of operators overloading and modification
            of their meaning.

            Comment

            • Steve Holden

              #7
              Re: double underscore attributes?

              bobueland@yahoo .com wrote:[color=blue]
              > Entering
              >[color=green][color=darkred]
              >>>>dir(5)[/color][/color]
              >
              >
              > I get
              > ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
              > '__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
              > '__floordiv__', '__getattribute __', '__getnewargs__ ', '__hash__',
              > '__hex__', '__init__', '__int__', '__invert__', '__long__',
              > '__lshift__', '__mod__', '__mul__', '__neg__', '__new__',
              > '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__',
              > '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__' ,
              > '__repr__', '__rfloordiv__' , '__rlshift__', '__rmod__', '__rmul__',
              > '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__',
              > '__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__',
              > '__truediv__', '__xor__']
              >
              > Every time I use dir(some module) I get a lot of attributes with double
              > underscore, for example __add__. Ok, I thought __add__ must be a method
              > which I can apply like this
              >[color=green][color=darkred]
              >>>>5.__add(8 )[/color][/color]
              >[/color]
              Bzzt.

              Python 2.4.1 (#1, May 27 2005, 18:02:40)
              [GCC 3.3.3 (cygwin special)] on cygwin
              Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
              >>> 5 . __add__(9)[/color][/color][/color]
              14[color=blue][color=green][color=darkred]
              >>>[/color][/color][/color]

              [...]
              regards
              Steve
              --
              Steve Holden +44 150 684 7255 +1 800 494 3119
              Holden Web LLC www.holdenweb.com
              PyCon TX 2006 www.python.org/pycon/

              Comment

              Working...