Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9200835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:51:22+00:00 2026-06-17T22:51:22+00:00

I’m creating a function tagging system, to enable or disable functions based on tags:

  • 0

I’m creating a function tagging system, to enable or disable functions based on tags:

def do_nothing(*args, **kwargs): pass

class Selector(set):
    def tag(self, tag):
        def decorator(func):
            if tag in self:
                return func
            else:
                return do_nothing
        return decorator

selector = Selector(['a'])

@selector.tag('a')
def foo1():
    print "I am called"

@selector.tag('b')
def foo2():
    print "I am not called"

@selector.tag('a')
@selector.tag('b')
def foo3():
    print "I want to be called, but I won't be"

foo1() #Prints "I am called"
foo2() #Does nothing
foo3() #Does nothing, even though it is tagged with 'a'

My question is about the last function, foo3. I understand why it isn’t being called. I was wondering if there’s a way to make it so that it is called if any of the tags are present in the selector. Ideally, the solution makes it so the tags are only checked once, not every time the function is called.

A side note: I’m doing this to select tests to run based on environment variables in unittest unit tests. My actual implementation uses unittest.skip.

EDIT: Added the decorator return.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:51:24+00:00Added an answer on June 17, 2026 at 10:51 pm

    The issue is that if you decorate it twice, one returns the function, one returns nothing.

    foo3() -> @selector.tag('a') -> foo3()
    foo3() -> @selector.tag('b') -> do_nothing
    
    foo3() -> @selector.tag('b') -> do_nothing
    do_nothing -> @selector.tag('a') -> do_nothing
    

    This means, in whatever order, you will always get nothing. What you need to do is keep a set of tags on each object, and check that whole set at once. We can do this nicely without polluting namespaces with function attributes:

    class Selector(set):
        def tag(self, *tags):
            tags = set(tags)
            def decorator(func):
                if hasattr(func, "_tags"):
                    func._tags.update(tags)
                else:
                    func._tags = tags
                @functools.wraps(func)
                def wrapper(*args, **kwargs):
                    return func(*args, **kwargs) if self & func._tags else None
                wrapper._tags = func._tags
                return wrapper
            return decorator
    

    This gives some bonuses – it’s possible to inspect all the tags the function has, and it’s possible to tag with multiple decorators or by giving many tags in a single decorator.

    @selector.tag('a')
    @selector.tag('b')
    def foo():
        ...
    
    
    #Or, equivalently:
    @selector.tag('a', 'b')
    def foo():
        ...
    

    The use of functools.wraps() also means the function keeps it’s original ‘identity’ (docstrings, name, etc…).

    Edit: If you wanted to do some wrapper elimination:

        def decorator(func):
            if hasattr(func, "_tagged_function"):
                func = func._tagged_function
            if hasattr(func, "_tags"):
                func._tags.update(tags)
            else:
                func._tags = tags
            @functools.wraps(func)
            def wrapper(*args, **kwargs):
                return func(*args, **kwargs) if self & func._tags else None
            wrapper._tagged_function = func
            wrapper._tags = func._tags
            return wrapper
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.