overriding '=' operator?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • user@domain.invalid

    overriding '=' operator?

    Hi,

    Is it possible to override assignment, the
    way that '+' can be overridden for example?

    Thanks,

    Toby

  • Erik Max Francis

    #2
    Re: overriding '=' operator?

    user@domain.inv alid wrote:
    [color=blue]
    > Is it possible to override assignment, the
    > way that '+' can be overridden for example?[/color]

    You can in statements like

    a[i] = something

    or

    a.x = something

    by overriding methods on the a object, but you cannot override the
    behavior for

    a = something

    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ After a thousand years or so you go native.
    \__/ Camden Benares

    Comment

    • John Roth

      #3
      Re: overriding '=' operator?


      <user@domain.in valid> wrote in message
      news:5ef57a632c 39376e860dfdd25 11451ae@news.te ranews.com...[color=blue]
      > Hi,
      >
      > Is it possible to override assignment, the
      > way that '+' can be overridden for example?[/color]

      No, because "=" isn't an operator, it's a statement.

      John Roth[color=blue]
      >
      > Thanks,
      >
      > Toby
      >[/color]


      Comment

      • Stephen Horne

        #4
        Re: overriding '=' operator?

        On Thu, 30 Oct 2003 20:32:52 GMT, user@domain.inv alid wrote:
        [color=blue]
        >Hi,
        >
        >Is it possible to override assignment, the
        >way that '+' can be overridden for example?[/color]

        No, not for the simple 'variable = value' case, because '=' doesn't
        modify a value.

        In Python, assignment binds the RHS value to the LHS name. The type of
        the previous LHS value is irrelevant - in Python, the type is a
        property of the value rather than the variable.

        The object previously bound to that variable is unaffected (apart from
        reference counting and possible garbage collection) and isn't really
        involved in the assignment. And as it isn't involved, it makes no
        sense for it to define what the assignment operator does.

        In short, you should think of the assignment operator as assigning to
        the variable - not to whatever object that variable happened to be
        bound to before the assignment.

        This is, of course, very different to statically typed languages like
        Ada and C++ where it makes sense for the assignment operator to be
        overridden based on the type - the type being associated with the
        variable, in these cases, rather than the value that happens to be
        stored there.

        If you really want to override assignment, one way would be to define
        a property. Instead of 'var = value' you would then write
        'var.propertyna me = value'. Using a property, assignments are
        translated into calls to getter or setter methods, which can be
        overridden fairly easily, and which can modify the object 'in place'.


        --
        Steve Horne

        steve at ninereeds dot fsnet dot co dot uk

        Comment

        • Terry Reedy

          #5
          Re: overriding '=' operator?


          <user@domain.in valid> wrote in message
          news:5ef57a632c 39376e860dfdd25 11451ae@news.te ranews.com...[color=blue]
          > Is it possible to override assignment, the
          > way that '+' can be overridden for example?[/color]

          In Python, = is *not* an operator. It is more like a statement
          keyword.

          tjr



          Comment

          Working...