[Python-ideas] unify usage of mutable and immutable objects

语言破碎处 mlet_it_bew at 126.com
Tue Feb 28 08:59:51 EST 2017


Hi!
    I write a '<'-based immutable set class.
    But it is quit different from the standard set class.
    I wish collections.abc.Set be more friendly to immutable tree sets or
    Python add new syntax to unify such difference.


good example:
    a = []
    a += a  # "a" is the original list

    a = ()
    a += a  # "a" is a new tuple

bad example:
    a = set()
    a.add(1)        # return None; "a" changed
    e = a.pop();

    a = frozen_tree_set()
    a = a.add(1)    # return another set;
    e, a = a.ipop() # return two objects instead of one!

solution I used:
    a <<= 1         # <==> a = a.add(1)
    but "a.ipop()" ....
my current solution is to write a wrapper class
    to turn immutable set into mutable set,
    but it is so noisy to box and unbox.


solution that I wish:
    a :=.add(1)      # "=." mimic "+="; return the result object
    e ~ a :=.pop()

    d[key] :=.add(1) # in dict



if only python add following features:
1) modify the original object
    1-0)
        1) define:
        def .method(self):...
        # "." means "intend to modify self"
        #       return any thing
        # to avoid immutable method
        #       which was intended to return new object
        #       leave self unchanged

        2) invoke:
        r = a..method();
    1-1) ignore result
        # no matter what returned, discard it
        a.-.method();           # <==> [a..method(), None][-1]
    1-2) return self
        # no matter what returned, return self
        a.>.method().>.method();# <==> [a..method(), a..method(), a][-1]


2) create new object
    2-0)
        1) define
        # intend to return (result, new object)
        def ^method():...

        2)invoke:
        r, a' = a.^method();
    2-1) return other, discard result
        a.-^method().-^method();# <==> a.^method()[1].^method()[1];
    2-2) assign other to original variable
        a=^method();            # <==> a = a.^method()[1];

3) unify both:
    a :=.method();
    # if   defined ".method" then "a..method();"
    # elif defined "^method" then "a = a.^method()[1];"
    # elif defined  "method" then "a.method();"
    # else error

    r ~ a :=.method();
    # if   defined ".method" then "r = a..method();"
    # elif defined "^method" then "r, a = a.^method();"
    # elif defined  "method" then "r = a.method();"
    # else error




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170228/9c30affa/attachment-0001.html>


More information about the Python-ideas mailing list