Development

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Overloading assignment operator

    12 answers - 487 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hi,
    I want to use Python to script some formulas in my application. The user
    should be able to write something like
    A = B * C
    where A,B,C are instances of some wrapper classes. * is no
    problem but I cannot overload the assignment of A. I understand that
    this is due to the nature of Python, but is there a trick to work around
    this?
    All I'm interested in is a clean syntax to script my app. Any ideas are
    very welcome.
    regards,
    Achim
  • No.1 | | 607 bytes | |

    Achim Domma wrote:

    I want to use Python to script some formulas in my application. The user
    should be able to write something like

    A = B * C

    where A,B,C are instances of some wrapper classes. * is no
    problem but I cannot overload the assignment of A. I understand that
    this is due to the nature of Python, but is there a trick to work around
    this?

    class D(dict):
    def __setitem__(self, key, value):
    print key, "<--", value
    dictsetitem__(self, key, value)

    namespace = D(B=42, C=24)
    exec "A = B * C" in namespace
    A <-- 1008

    Peter
  • No.2 | | 1057 bytes | |

    Jan 23, 12:24 pm, Achim Domma <d (AT) procoders (DOT) netwrote:
    Hi,

    I want to use Python to script some formulas in my application. The user
    should be able to write something like

    A = B * C

    where A,B,C are instances of some wrapper classes. * is no
    problem but I cannot overload the assignment of A. I understand that
    this is due to the nature of Python, but is there a trick to work around
    this?
    All I'm interested in is a clean syntax to script my app. Any ideas are
    very welcome.

    regards,
    Achim

    Simple option: how do you feel about using '<<=' instead of '=' (by
    defining __ilshift__)? This gives you:

    A <<= B * C

    (looks sort of like "injecting" the result of B times C into A)

    More complicated option: embed an expression/assignment parser into
    your app. You can get a jump on this using some of the examples that
    come with pyparsing (can check these out online at
    - look at fourFn.py and
    simpleArith.py).
    -- Paul
  • No.3 | | 745 bytes | |

    Achim Domma wrote:

    I want to use Python to script some formulas in my application. The user
    should be able to write something like

    A = B * C

    where A,B,C are instances of some wrapper classes. * is no
    problem but I cannot overload the assignment of A. I understand that
    this is due to the nature of Python, but is there a trick to work around
    this?
    All I'm interested in is a clean syntax to script my app. Any ideas are
    very welcome.

    Are you sure you even need to do that?

    class C:
    def __init__(self, x):
    self.x = x
    def __mul__(self, other):
    return C(self.x*other.x)

    result = C(2)*C(3)
    print result
    <__mainC instance at 0x402e13ec>
    result.x
    6
  • No.4 | | 508 bytes | |

    Achim Domma schrieb:

    Hi,

    I want to use Python to script some formulas in my application. The user
    should be able to write something like

    A = B * C

    where A,B,C are instances of some wrapper classes. * is no
    problem but I cannot overload the assignment of A. I understand that
    this is due to the nature of Python, but is there a trick to work around
    this?

    Not that I know about it but what shall be the behaviour of assignment
    when being overloaded?

    Kay
  • No.5 | | 1527 bytes | |

    Tue, 23 Jan 2007 19:42:01 +0100, Peter wrote:

    Achim Domma wrote:

    >I want to use Python to script some formulas in my application. The user
    >should be able to write something like
    >
    >A = B * C
    >
    >where A,B,C are instances of some wrapper classes. * is no
    >problem but I cannot overload the assignment of A. I understand that
    >this is due to the nature of Python, but is there a trick to work around
    >this?


    class D(dict):
    def __setitem__(self, key, value):
    print key, "<--", value
    dictsetitem__(self, key, value)

    namespace = D(B=42, C=24)
    exec "A = B * C" in namespace
    A <-- 1008

    Very clever, except:

    (1) The Poster's requirement was for a "clean syntax" and
    'exec "A = B * C" in namespace' is anything but a clean syntax.

    (2) The P. specifies that the syntax is for use by his users. We don't
    know who these users are, but can you see users getting this right and not
    ignoring the namespace argument?

    (3) Even if they do use the namespace argument, how hard is it for the
    users to break the security of your exec?

    exec "A = B * C;import os;os.system('ls -l break-something')" in namespace
    A <-- 1008
    os <-- <module 'os' from '/usr/lib/python2.4/os.pyc'>
    -rw-rw-r-- 1 steve steve 0 Jan 24 08:27 break-something

    Using exec on user-supplied data is just begging to be p0wned.
  • No.6 | | 346 bytes | |

    Paul McGuire wrote:

    Simple option: how do you feel about using '<<=' instead of '=' (by
    defining __ilshift__)? This gives you:

    A <<= B * C

    (looks sort of like "injecting" the result of B times C into A)

    Thanks! That is exactly the kind of solution I was looking for! :-)

    Achim
  • No.7 | | 1882 bytes | |

    Steven D'Aprano wrote:

    Tue, 23 Jan 2007 19:42:01 +0100, Peter wrote:

    >Achim Domma wrote:
    >

    I want to use Python to script some formulas in my application. The user
    should be able to write something like

    A = B * C

    where A,B,C are instances of some wrapper classes. * is no
    problem but I cannot overload the assignment of A. I understand that
    this is due to the nature of Python, but is there a trick to work around
    this?

    class D(dict):
    >def __setitem__(self, key, value):
    >print key, "<--", value
    >dictsetitem__(self, key, value)
    >

    namespace = D(B=42, C=24)
    exec "A = B * C" in namespace
    >A <-- 1008


    Very clever, except:

    (1) The Poster's requirement was for a "clean syntax" and
    'exec "A = B * C" in namespace' is anything but a clean syntax.

    (2) The P. specifies that the syntax is for use by his users. We don't
    know who these users are, but can you see users getting this right and not
    ignoring the namespace argument?

    I thought he might hide everything but the expression

    A = B * C

    from the user.

    (3) Even if they do use the namespace argument, how hard is it for the
    users to break the security of your exec?

    exec "A = B * C;import os;os.system('ls -l break-something')" in
    namespace
    A <-- 1008
    os <-- <module 'os' from '/usr/lib/python2.4/os.pyc'>
    -rw-rw-r-- 1 steve steve 0 Jan 24 08:27 break-something

    Using exec on user-supplied data is just begging to be p0wned.

    Yes. Unless the application is deployed to the user's machine, in which case
    he has more straightforward methods to destroy his own data.

    Peter
  • No.8 | | 715 bytes | |

    Achim Domma wrote:
    Hi,

    I want to use Python to script some formulas in my application. The user
    should be able to write something like

    A = B * C

    where A,B,C are instances of some wrapper classes. * is no
    problem but I cannot overload the assignment of A. I understand that
    this is due to the nature of Python, but is there a trick to work around
    this?
    All I'm interested in is a clean syntax to script my app. Any ideas are
    very welcome.

    regards,
    Achim

    Why do you need to overload assignment anyway? If you overloaded "*"
    properly, it should return
    the result you want, which you then "assign" to A as usual. Maybe I'm
    missing something.
  • No.9 | | 1918 bytes | |

    Tue, 23 Jan 2007 18:07:55 -0800, Russ wrote:

    Achim Domma wrote:
    >Hi,
    >>

    >I want to use Python to script some formulas in my application. The user
    >should be able to write something like
    >>

    >A = B * C
    >>

    >where A,B,C are instances of some wrapper classes. * is no
    >problem but I cannot overload the assignment of A. I understand that
    >this is due to the nature of Python, but is there a trick to work around
    >this?
    >All I'm interested in is a clean syntax to script my app. Any ideas are
    >very welcome.
    >>

    >regards,
    >Achim


    Why do you need to overload assignment anyway? If you overloaded "*"
    properly, it should return
    the result you want, which you then "assign" to A as usual. Maybe I'm
    missing something.

    common reason for overriding assignment is so the left-hand-side of
    the assignment can choose the result type. E.g. if Cheddar, Swiss and
    Wensleydale are three custom classes, mutually compatible for
    multiplication:

    B = Cheddar() # B is type Cheddar
    C = Swiss() # C is type Swiss
    # without overloading assignment
    A = B * C # A is (possibly) Cheddar since Bmul__ is called first
    A = C * B # A is (possibly) Swiss since Cmul__ is called first
    # with (hypothetical) assignment overloading
    A = B * C # A is type Wensleydale since Aassign__ is called

    Except, of course, there is no assignment overloading in Python. There
    can't be, because A may not exist when the assignment is performed, and
    if it does exist it might be a complete different type.

    Instead, you can do something like this:

    A = Wensleydale(B) * Wensleydale(C)

    or

    A = Wensleydale(B * C)
  • No.10 | | 407 bytes | |

    Hi thre,

    Jan 24, 5:24 am, Achim Domma <d (AT) procoders (DOT) netwrote:
    I want to use Python to script some formulas in my application.

    Depending on what you're trying to do, you might possibly find it
    useful to lake a look at the approach used by PyGINAC, which is a
    symbolic algebra system (in C++) wrapped with some clever python
    bindings:

    Hope that helps,
    JP
  • No.11 | | 2033 bytes | |

    Steven D'Aprano wrote:
    Tue, 23 Jan 2007 18:07:55 -0800, Russ wrote:

    >Achim Domma wrote:

    Hi,

    I want to use Python to script some formulas in my application. The user
    should be able to write something like

    A = B * C

    where A,B,C are instances of some wrapper classes. * is no
    problem but I cannot overload the assignment of A. I understand that
    this is due to the nature of Python, but is there a trick to work around
    this?
    All I'm interested in is a clean syntax to script my app. Any ideas are
    very welcome.

    regards,
    Achim
    >Why do you need to overload assignment anyway? If you overloaded "*"
    >properly, it should return
    >the result you want, which you then "assign" to A as usual. Maybe I'm
    >missing something.


    common reason for overriding assignment is so the left-hand-side of
    the assignment can choose the result type. E.g. if Cheddar, Swiss and
    Wensleydale are three custom classes, mutually compatible for
    multiplication:

    B = Cheddar() # B is type Cheddar
    C = Swiss() # C is type Swiss
    # without overloading assignment
    A = B * C # A is (possibly) Cheddar since Bmul__ is called first
    A = C * B # A is (possibly) Swiss since Cmul__ is called first
    # with (hypothetical) assignment overloading
    A = B * C # A is type Wensleydale since Aassign__ is called

    Except, of course, there is no assignment overloading in Python. There
    can't be, because A may not exist when the assignment is performed, and
    if it does exist it might be a complete different type.

    Instead, you can do something like this:

    A = Wensleydale(B) * Wensleydale(C)

    or

    A = Wensleydale(B * C)

    I think that's the first time I've actually seen someone use a Monty
    Python theme for a python example, and I must say, I like it. However,
    "We are all out of Wensleydale."

    Cheers,
    Cliff
  • No.12 | | 358 bytes | |

    J. Clifford Dyer wrote:
    I think that's the first time I've actually seen someone use a Monty
    Python theme for a python example, and I must say, I like it. However,
    "We are all out of Wensleydale."

    Cheers,
    Cliff

    , then you clearly don't waste nearly enough time on this newsgroup ;-)

    Idly yours,

    Michael

Re: Overloading assignment operator


max 4000 letters.
Your nickname that display:
In order to stop the spam: 4 + 3 =
QUESTION ON "Development"

EMSDN.COM