a = b = 1 just syntactic sugar?

Steven Taschuk staschuk at telusplanet.net
Thu Jun 5 20:27:11 CEST 2003

Quoth Ed Avis:
> 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.  [...]

What I had in mind for the __init__ problem was using, say,
    getattr(cfg, 'command_' + cmd)(arg)
so that only methods named in a certain way were usable as
commands.  Of course, it's even better to move this dispatch logic
into the object, so you'd have

    class config(object):
        def command_setcolour(self, value):     
            # ...
        # etc.
        def perform(self, command, arg):
            getattr(self, 'command_' + command)(arg)

whereupon reading the configuration is just

    cfg = config()
    for cmd, arg in commands:
        cfg.perform(cmd, arg)

> [...] 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.

Yes, I see your point.  However, I don't think anything can be
done about it without throwing away the present use of indentation
for block delimiting.

Also, a scheme such as the above, with an explicit configuration
object, seems quite natural to me.

Steven Taschuk             "The world will end if you get this wrong."
staschuk at telusplanet.net     -- "Typesetting Mathematics -- User's Guide",
                                 Brian Kernighan and Lorrinda Cherry

More information about the Python-list mailing list