script question
Scott David Daniels
Scott.Daniels at Acm.Org
Fri Apr 17 15:30:37 EDT 2009
python at bdurham.com wrote:
> Scott,
>
> Newbie question (and I'm not the OP): What are your thoughts on having
> your decorator add an attribute to the functions vs. placing the
> functions in a global variable?
>
> def _included(f):
> f._included = True
> return f
>
> I tried experimenting with this technique, but could not find a way to
> use dir() to discover all my functions with an _included attribute.
>
> The following convoluted comprehension works to execute functions with
> an _included attribute when a list of function names is available....
> However, I can't figure out a way to replace my list of hard-coded
> function names with the output from the dir() command.
Here's how I'd do it:
def call_all(module):
for name in dir(module):
contents = getattr(module, name)
if hasattr(contents, '_included'):
yield contents()
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list