[Interface-dev] Re: interface.py
Jim Fulton
jim at zope.com
Mon Jul 11 07:12:45 EDT 2005
Christopher Armstrong wrote:
> On 7/11/05, Thomas Lotze <tl at gocept.com> wrote:
>
>>Steve Alexander wrote:
>>
>>
>>>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.
>>
>>OK, no arguing against that. Maybe I played around too much with closures
>>lately, so I didn't realize how unclear it was. However, I think the
>>explanation needn't necessarily rant on evility where it actually just
>>takes knowing why thing are done the way they are done.
Given the extensive comment, I don't think it's unclear at all.
If a reader doesn't understand the implementation, they should move
on and read some other code they do understand. The comment
leaves no doubt that something special is going on.
> That weird trick seems totally unnecessary to me; I don't see why
> whoever wrote it thought it was so horrible to have a module-global
> name bound -- just don't stick it in __all__. Plus, it took me *weeks*
> to grok why it was used at all, even given the explanation in the code
> (probably mostly because it never occurred to me that it was a good
> reason ;)
I didn't like using a global for a few reasons:
1. It splits up the implementation. I needed a marker so I
could tell if something had been passed. If I use a global,
then the marker has to be defined far away from the function
that needs it.
2. We've had situations where people tried to reuse markers accross
modules. People saw the marker in the module and thought they could
use it. This led to some really hard to debug bugs.
3. I thought that using a cell would be faster than using a global.
Tim Peters recently demonstrated that in current Python, this is not
the case. (Maybe it never was.)
In general, I don't like using an optional argument to change the semantics
of a routine, because implementing optional arguments is so tricky in Python.
(It's very easy in C.)
> Anyway, point is I think it was too bizarre a trick to really warrant
> the non-issue of the module-global name.
<shrug> I don't really agree. Generally, I'd say that this sort of
optional parameter is too tricky to be worth the benefit. I made
an exception here because I find interface call so compelling. If you
*are* going to use optional arguments like this, I much prefer
a solution like this because it's easier to reason about. You absolutely
know where the marker is defined and that no one can mess with it.
An alternative approach for the default argument would be to require a
keyword argument. This could have a much simpler implementation
and allow multi-adatation.
Jim
--
Jim Fulton mailto:jim at zope.com Python Powered!
CTO (540) 361-1714 http://www.python.org
Zope Corporation http://www.zope.com http://www.zope.org
More information about the Interface-dev
mailing list