overriding = operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anton Mellit

    overriding = operator

    Hi,

    I am developing something like a compiler in Python, a library that
    would help to generate machine-language code. One of the features is
    the following. When I want to generate a piece of code I want to
    declare variables as follows:

    x = var()
    y = var()

    This would generate no code, but it would mean that I need, say, two
    32-bit integer variables. Then whenever I write something like x+y,
    the '+' operator is overriden in such a way that a code which computes
    the sum is generated. What I also want to do, I want to write
    something like

    z = var()
    z = x + y

    and I want a code which takes the sum of x and y and puts it in z to
    be generated. However in python z = x + y already has its meaning and
    it does something different from what I want. So I need something like
    'overriding' =, which is impossible, but I look for a systematic
    approach to do something instead. It seems there are two ways to do
    what I need:

    1. Implement a method 'assign' which generates the corresponding code
    to store value:

    z.assign(x + y)

    2. Do the same as 1., but via property set methods. For example, this
    would look cleaner:

    z.value = x + y

    Which of these is preferrable? Does anyone know any alternative ways?

    Anton
  • Paul McGuire

    #2
    Re: overriding = operator

    On Apr 22, 8:47 am, "Anton Mellit" <mel...@gmail.c omwrote:
    I need something like
    'overriding' =, which is impossible, but I look for a systematic
    approach to do something instead. It seems there are two ways to do
    what I need:
    >
    1. Implement a method 'assign' which generates the corresponding code
    to store value:
    >
    z.assign(x + y)
    >
    2. Do the same as 1., but via property set methods. For example, this
    would look cleaner:
    >
    z.value = x + y
    >
    Which of these is preferrable? Does anyone know any alternative ways?
    >
    Anton
    If you are willing to accept '<<=' as meaning 'assign to existing'
    instead of 'left shift in place', you can override this operator using
    the __ilshift__ method.

    We used this technique in C++/CORBA code back in the 90's to "inject"
    values into CORBA::Any variables.

    -- Paul

    Comment

    • Anton Mellit

      #3
      Re: overriding = operator

      On Apr 22, 3:54 pm, Paul McGuire <pt...@austin.r r.comwrote:
      On Apr 22, 8:47 am, "Anton Mellit" <mel...@gmail.c omwrote:
      >
      >
      >
      I need something like
      'overriding' =, which is impossible, but I look for a systematic
      approach to do something instead. It seems there are two ways to do
      what I need:
      >
      1. Implement a method 'assign' which generates the corresponding code
      to store value:
      >
      z.assign(x + y)
      >
      2. Do the same as 1., but via property set methods. For example, this
      would look cleaner:
      >
      z.value = x + y
      >
      Which of these is preferrable? Does anyone know any alternative ways?
      >
      Anton
      >
      If you are willing to accept '<<=' as meaning 'assign to existing'
      instead of 'left shift in place', you can override this operator using
      the __ilshift__ method.
      >
      We used this technique in C++/CORBA code back in the 90's to "inject"
      values into CORBA::Any variables.
      >
      -- Paul
      Great idea! It conflicts with 'left shift in place', which i also
      need, but '=' seems more important.

      Anton

      Comment

      Working...