Python declarative

Chris Angelico rosuav at gmail.com
Wed Jan 15 18:09:47 EST 2014


On Thu, Jan 16, 2014 at 9:58 AM, Terry Reedy <tjreedy at udel.edu> wrote:
> class Window:
>     def __init__(self, title, *kwds)  # or title='Window title'
>         self.title = title
>         self.__dict__.update(kwds)

Does that want a second asterisk, matching the Button definition?

>> Possible, but potentially messy; if you happen to name your button
>> "icon", it might be misinterpreted as an attempt to set the window's
>> icon, and cause a very strange and incomprehensible error.
>
> Puns are always a problem with such interfaces. Validate the args as much as
> possible. An icon should be a bitmap of appropriate size. Optional args
> should perhaps all be widgets (instances of a Widget baseclass).

Yeah, but you'd still get back an error saying "icon should be a
bitmap" where the real problem is "icon should be called something
else". It might be worth explicitly adorning properties, or separating
them into two categories. Since the keyword-named-children system has
the other problem of being hard to lay out (how do you specify the
order?), I'd look at keyword args for properties and something
separate for children - either the layout I used above with .add(),
which allows extra args as necessary, or something like this:

myWindow = Window(
     title="Hello World",
     children=[Button(
         label="I'm a button",
         onClick=exit
     )]
)

Or maybe allow "child=" as a shortcut, since a lot of widgets will
have exactly one child.

ChrisA



More information about the Python-list mailing list