[Python-ideas] "Immutable Builder" Pattern and Operator

Soni L. fakedme+py at gmail.com
Mon Jan 23 13:32:12 EST 2017



On 23/01/17 04:27 PM, Gerald Britton wrote:
>
>
> On Jan 23, 2017 1:12 PM, "Britton, Gerald" <Gerald.Britton at td.com 
> <mailto:Gerald.Britton at td.com>> wrote:
>
>     On 23/01/17 02:56 PM, Gerald Britton wrote:
>
>     >//
>
>     >//
>
>     >/On Jan 23, 2017 11:07 AM, "Soni L." <fakedme+py at gmail.com
>     <https://mail.python.org/mailman/listinfo/python-ideas> /
>
>     >/<mailto:fakedme% <mailto:fakedme%25>2Bpy at gmail.com
>     <https://mail.python.org/mailman/listinfo/python-ideas>>> wrote:/
>
>     >//
>
>     >//
>
>     >//
>
>     >/     On 23/01/17 01:52 PM, Gerald Britton wrote:/
>
>     >>//
>
>     >>//
>
>     >>//
>
>     >>/         [snip]/
>
>     >>//
>
>     >>/         >I propose `x .= y` -> `x = x . y`, for any `y`./
>
>     >>//
>
>     >>/         [snip]/
>
>     >>//
>
>     >>/         I think you mean "any y that is a member of x"/
>
>     >>//
>
>     >//
>
>     >/     Since it desugars into `x = x.y`, you can literally use
>     anything/
>
>     >/     for `y`./
>
>     >//
>
>     >/     x .= __call__().whatever().unwrap() * 3/
>
>     >//
>
>     >/     is equivalent to/
>
>     >//
>
>     >/     x = x.__call__().whatever().unwrap() * 3/
>
>     >//
>
>     >/     and/
>
>     >//
>
>     >/     x .= 1/
>
>     >//
>
>     >/     is equivalent to/
>
>     >//
>
>     >/     x = x.1/
>
>     >//
>
>     >/     which is equivalent to/
>
>     >//
>
>     >/     SyntaxError: invalid syntax/
>
>     >//
>
>     >//
>
>     >>/         Also, note that this syntax means that x will be rebound to/
>
>     >>/         the result of calling x.y, whatever that is (frequently,/
>
>     >>/         None, for mutating methods)/
>
>     >>//
>
>     >>/         In general, you can't count on methods to return references/
>
>     >>/         to their instances, even though it's handy for fluent
>     coding,/
>
>     >>/         so this side effect may be unexpected to some/
>
>     >>//
>
>     >//
>
>     >/     This is why it's for use with **immutable** objects./
>
>     >//
>
>     >//
>
>     >>/         That's a problem with your original example:/
>
>     >>//
>
>     >>/         >long_name = mkbuilder()/
>
>     >>//
>
>     >>/         >long_name = long_name.seta(a)/
>
>     >>//
>
>     >>/         >long_name = long_name.setb(b)/
>
>     >>//
>
>     >>/         >y = long_name.build()/
>
>     >>//
>
>     >>/         What do the methods seta and setb return?  If they don't/
>
>     >>/         return "self" you've got a problem. I think./
>
>     >>//
>
>     >//
>
>     >/     They don't return self. Ever. The value bound to long_name is/
>
>     >/     immutable, like an integer. They return a new instance./
>
>     >//
>
>     >//
>
>     >/Then long_name isn't immutable. It changes with every line. That
>     can /
>
>     >/lead to nasty bugs if you count on its immutability./
>
>     >//
>
>     >/Easy to see. Just print long_name after each call./
>
>     You're mixing up value immutability with name immutability. The name
>
>     isn't immutable, but:
>
> Er...No. I'm not confused at all, unless you define immutability in a 
> new way.. you said that the "value bound to long_name is immutable." 
> It's not. Your example below proves it.
> An immutable object is one whose state cannot be modified once set. 
> That's not happening here.  The state of the object bound to 
> long_name, which is a pointer to an instance of you class, changes 
> with each line.
Python has pointers now?! The value pointed to by the value bound to 
long_name is immutable. Can you stop being so pedantic? >.<
>
>     long_name = mkbuilder()
>
>     x = long_name
>
>     long_name .= seta("a")
>
>     y = long_name
>
>     long_name .= setb("b")
>
>     z = long_name
>
>     print(x)  # a = None, b = None
>
>     print(y)  # a = "a", b = None
>
>     print(z)  # a = "a", b = "b"
>
>     print(x is y)  # False
>
>     print(x is z)  # False
>
>     print(y is z)  # False
>
>     print(long_name is z)  # True
>
>     See also:
>
>     long_name = 1
>
>     x = long_name
>
>     long_name += 1
>
>     y = long_name
>
>     long_name += 1
>
>     z = long_name
>
>     print(x)  # 1
>
>     print(y)  # 2
>
>     print(z)  # 3
>
>     print(x is y)  # False
>
>     print(x is z)  # False
>
>     print(y is z)  # False
>
>     print(long_name is z)  # True
>
>     >//
>
>     >//
>
>     >//
>
>     >>/         FWIW why can't you just write:/
>
>     >>//
>
>     >>/         x.y/
>
>     >>//
>
>     >>/         or for your example:/
>
>     >>//
>
>     >>/         long_name.seta(a)/
>
>     >>//
>
>     >>/         ?/
>
>     >>//
>
>     >>//
>
>     >//
>
>     >/     See the IRC bot builder example, it should be more clear. (It's/
>
>     >/     about forking the builder.)/
>
>     >//
>
>     >//
>
>     If you wish to unsubscribe from receiving commercial electronic
>     messages from TD Bank Group, please click here
>     <http://www.td.com/tdoptout> or go to the following web address:
>     www.td.com/tdoptout <http://www.td.com/tdoptout> Si vous souhaitez
>     vous désabonner des messages électroniques de nature commerciale
>     envoyés par Groupe Banque TD veuillez cliquer ici
>     <http://www.td.com/tddesab> ou vous rendre à l'adresse
>     www.td.com/tddesab <http://www.td.com/tddesab>
>
>     NOTICE: Confidential message which may be privileged. Unauthorized
>     use/disclosure prohibited. If received in error, please go to
>     www.td.com/legal <http://www.td.com/legal> for instructions. AVIS
>     : Message confidentiel dont le contenu peut être privilégié.
>     Utilisation/divulgation interdites sans permission. Si reçu par
>     erreur, prière d'aller au www.td.com/francais/avis_juridique
>     <http://www.td.com/francais/avis_juridique> pour des instructions.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170123/00cf8901/attachment.html>


More information about the Python-ideas mailing list