Python 2.7 - clean syntax for lvalue modification -


it common have struct-like types not expected modified distant copyholders.

a string basic example, that's easy case because it's excusably immutable -- python unusual in allowing things method calls on literal strings.

the problem (in languages) have things like, (x,y) point class. want change x , y independently. i.e., usage perspective, point lvalue should mutable (even though copies not see mutation).

but python 2.7 doesn't seem provide options enable automatic copy-on-assignment. means must make our point class immutable because inadvertent references going created on place (typically because forgot clone object before passing else).

and no, i'm not interested in countless hacks allow object mutated "while it's being created", weak concept not scale.

the logical conclusion of these circumstances need our mutation methods modify lvalue. example %= supports that. problem better have more reasonable syntax, using __setattr__ and/or defining set_x , set_y methods, shown below.

class point(object): # python doesn't have copy-on-assignment, must use immutable # object avoid unintended changes distant copyholders.      def __init__(self, x, y, others=none):         object.__setattr__(self, 'x', x)         object.__setattr__(self, 'y', y)      def __setattr__(self, name, value):         self %= (name, value)         return self # should modify lvalue (didn't work)      def __repr__(self):         return "(%d %d)" % (self.x, self.y)      def copy(self, x=none, y=none):         if x == none: x = self.x         if y == none: y = self.y         return point(x, y)      def __eq__ (a,b): return a.x == b.x , a.y == b.y     def __ne__ (a,b): return a.x != b.x or  a.y != b.y     def __add__(a,b): return point(a.x+b.x, a.y+b.y)     def __sub__(a,b): return point(a.x-b.x, a.y-b.y)      def set_x(a,b): return a.copy(x=b) # should modify lvalue (didn't work)     def set_y(a,b): return a.copy(y=b) # should modify lvalue (didn't work)      # works in python 2.7. syntax awful.     def __imod__(a,b):         if   b[0] == 'x': return a.copy(x=b[1])         elif b[0] == 'y': return a.copy(y=b[1])         else:             raise attributeerror,  \                 "point has no member '%s'" % b[0]    my_very_long_and_complicated_lvalue_expression = [point(10,10)] * 4   # modify element 0 via "+="   -- ok my_very_long_and_complicated_lvalue_expression[0] += point(1,-1)  # modify element 1 via normal "__set_attr__"   -- not ok my_very_long_and_complicated_lvalue_expression[1].x = 9999  # modify element 2 via normal "set_x"  -- not ok my_very_long_and_complicated_lvalue_expression[2].set_x(99)  # modify element 3 via goofy "set_x"   -- ok my_very_long_and_complicated_lvalue_expression[3]    %='x',   999   print my_very_long_and_complicated_lvalue_expression 

the result is:

[(11 9), (10 10), (10 10), (999 10)] 

as can see, += , %= work fine, else doesn't seem work. surely language inventors have created basic syntax lvalue modification not limited goofy-looking operators. can't seem find it. please help.

in python, typical pattern copy before modification rather copying on assignment. implement kind of data store semantics want, seems lot of work.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -