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

Ethan Furman ethan at stoneleaf.us
Sun Jan 22 18:41:26 EST 2017


On 01/22/2017 03:30 PM, Soni L. wrote:
> On 22/01/17 08:54 PM, Serhiy Storchaka wrote:
>> On 23.01.17 00:45, Soni L. wrote:

>>> I've been thinking of an Immutable Builder pattern and an operator to go
>>> with it. Since the builder would be immutable, this wouldn't work:
>>>
>>> long_name = mkbuilder()
>>> long_name.seta(a)
>>> long_name.setb(b)
>>> y = long_name.build()
>>
>> I think the more pythonic way is:
>>
>> y = build(a=a, b=b)
>>
>> A Builder pattern is less used in Python due to the support of keyword arguments.
>
> I guess you could do something like this, for an IRC bot builder:
>
> fnircbotbuilder = mkircbotbuilder(network="irc.freenode.net", port=6697, ssl=true)
> mainbot = mkircbotbuilder(parent=fnircbotbuilder,  # ???
>                            channels=["#bots"]).build()
> fndccbotbuilder = mkircbotbuilder(parent=fnircbotbuilder, dcc=true, channeldcc=true)
> dccbot = mkircbotbuilder(parent=fndccbotbuilder, channels=["#ctcp-s"]).build()
> otherircbotbuilder = mkircbotbuilder(parent=fndccbotbuilder, network="irc.subluminal.net")  # because we want this whole network
> otherbot = mkircbotbuilder(parent=otherircbotbuilder, channels=["#programming"]).build()    # to use DCC and channel DCC

The following is just fine:

    fnircbotbuilder = mkircbotbuilder(
         network="irc.freenode.net",
         port=6697,
         ssl=true,
         )
     mainbot = fnircbotbuilder(channels=["#bots"]).build()

     fndccbotbuilder = fnircbotbuilder(dcc=true, channeldcc=true)
     dccbot = fndccbotbuilder(channels=["#ctcp-s"]).build()

     otherircbotbuilder = fndccbotbuilder(network="irc.subluminal.net")
     otherbot = otherircbotbuilder(channels=["#programming"]).build()

--
~Ethan~


More information about the Python-ideas mailing list