On Thu, Dec 11, 2014 at 6:55 PM, Roy Smith <r...@panix.com> wrote: > > In article <mailman.16880.1418342293.18130.python-l...@python.org>, > Ian Kelly <ian.g.ke...@gmail.com> wrote: > > > I never said that functions can't be used as namespaces. I said that > > functions are *bad* namespaces, and I gave reasons why I think this is true. > > An excellent example of functions acting as namespaces is nosetest's > @attr() decorator. We use this, for example, to tag certain test cases > as being reliant on facebook being up(*): > > @attr('facebook', 'services') > def test_some_facebook_thing(): > # whatever > > this lets us turn all those tests on or off with a single switch. The > way @attr() is implemented, it sets attributes on the decorated > function. It's the most logical and obvious place to store a piece of > information about a test case -- right on the test case itself.
I agree this is a great use of function attributes, but this is an example of tags or annotations, not namespaces. The purpose of namespaces is to distinguish between different entities of the same name, e.g. 'pow' and 'math.pow' are two distinct functions that both happen to be named pow. We can tell them apart because they're in separate namespaces. In the nosetest example, the 'facebook' attribute always means the same thing no matter which functions you apply it to. Conceptually speaking, each instance of the attribute is the same annotation. The expression test_some_facebook_thing.facebook doesn't perform the role of a namespace because it's used to signify the presence of that annotation, not to identify something. A similar example from the standard library is the functools.lru_cache decorator, which adds a cache_info function to the wrapped function. However, the cache_info function always performs the same task in relation to the particular function it decorates, so in this regard it acts more like an object method than like a name in a namespace.
-- https://mail.python.org/mailman/listinfo/python-list