a = b = 1 just syntactic sugar?

Ed Avis ed at membled.com
Wed Jun 4 23:10:51 CEST 2003

Steven Taschuk <staschuk at telusplanet.net> writes:

>    class config(object):
>        def __init__(self):
>            self.colour = 'blue'
>        def setcolour(self, value):
>            self.colour = value
>        def invertcolour(self, dummy):
>            self.colour = inverted[self.colour]
>        # ...
>    cfg = config()
>    for cmd, arg in commands:
>        getattr(cfg, cmd)(arg)
>    print 'colour is now', cfg.colour
>Some refinements are in order (e.g., to prevent the configuration
>file from invoking __init__), but the idea is clear.

Yes, that works, although I think I would prefer to define setcolour,
invertcolour and so on as stand-alone functions and then say

    table = {'setcolour': setcolour, ...}

which does duplicate the names, but takes care of the __init__ problem
at the same time, so it's not so bad.  It's just frustrating not to be
able to write the function definitions inline, in the obvious place,
especially when they might be only one line each and there is the
'lambda' keyword which at first looks so inviting and useful.

Ed Avis <ed at membled.com>

More information about the Python-list mailing list