Re: adding in-place operator to Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?UTF-8?B?R2VyaGFyZCBIw6RyaW5n?=

    Re: adding in-place operator to Python

    Arash Arfaee wrote:
    Hi All,
    >
    Is there anyway to add new in-place operator to Python?
    You can't create new syntax, like %=
    Or is there any way to redefine internal in-place operators?
    What you can do is give your objects the ability to use these operators.

    See http://docs.python.org/ref/numeric-types.html for __iadd_ (+=) and
    friends.

    You could implement something like a string buffer this way:

    class Buffer:
    def __init__(self):
    self.buf = []

    def __iadd__(self, item):
    self.buf.append (item)
    return self

    def __str__(self):
    return "".join(self.bu f)

    if __name__ == "__main__":
    buf = Buffer()
    buf += "str1"
    buf += "str2"

    print str(buf)

    -- Gerhard

Working...