[Interface-dev] interface.py

Steve Alexander steve at canonical.com
Mon Jul 11 02:45:04 EDT 2005


Hi Thomas,

Thanks for looking through the interfaces code.  It's one of those areas
that few people venture into, and it's really good when someone does.

I have comments on two of your points.  I haven't studied the code of
the rest.


> - Using a closure to implement InterfaceClass.__call__ is not IMO an
>   evil trick, it's a useful feature of the language.

I think it is an evil trick because it is unclear code.  Whenever
someone write unclear code but has a reason for not using a clear
alternative, it needs a comment to explain why.


> [1] There's actually one place where I wittingly changed behaviour in a
> way which IMO shouldn't matter: InterfaceClass.__init__ used to have a
> default argument attrs=None, which would be set to {} later. I think
> having attrs={} right as the default is the better solution.

That's a really bad thing to do.  With your change, the dict will be
instantiated once only, at the time the class is first read by the
interpreter.  As the dict is altered in the method, the default value
will change.

>>> def foo(a, b={}):
...     b[a] = 1
...     return b
...

>>> foo('hello')
{'hello': 1}

>>> foo('world')
{'world': 1, 'hello': 1}


You might expect this latter call to foo('world')
to return {'world': 1}.  To get this behaviour, I would have to do
something like clone the dict, or use None as the default value,

-- 
Steve Alexander


More information about the Interface-dev mailing list