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

  • Home
  • SEARCH
  • 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 8589219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:00:01+00:00 2026-06-11T23:00:01+00:00

I started out with something like this: class Client (object): def __init__ (self): self.state

  • 0

I started out with something like this:

class Client (object):
    def __init__ (self):
        self.state = None
    def open (self, destination):
        if self.state not in [None]: raise error ...
        ... open stuff ...
        self.state = 'Open'
    def handle (self):
        if self.state not in ['Open'] raise error ...
        ... handle stuff ...
    def close (self):
        if self.state not in ['Open'] raise error ...
        ... close stuff ...
        self.state = None

(I’m not happy about having separate __init__() and open() methods but the stuff I call requires things to be this way, I’m afraid. It’s not central to my question anyhow.)

Now, as the number of methods and states will be growing, I thought I should refactor into something like this:

    @states([None])
    def open (self, destination):
        ... open stuff ...

and similarly for the other methods. Based on e.g. this classic SO answer I came up with the following definition for the decorator:

from functools import wraps

def states (statelist):
    def decorator (f):
        @wraps(f)   # In order to preserve docstrings, etc.
        def wrapped (self, *args, **kwargs):
            if self.state not in statelist: raise error ...
            return f(self, *args, **kwargs)
        return wrapped
    return decorator

This is fairly complex, and also has the problem that it will not be inherited by derived classes (my solution was to simply make it a global). My question is: is this the minimal, idiomatic solution to this problem, or am I doing something wrong? This is the first time I’m trying to define my own decorator. The various references I have found (including this one which pointed me to wraps) seem to suggest to my uninformed self that this is really how it has to be. (Or is there a nifty library which encapsulates this sort of contortion for me? Had a quick glance at functools but I can’t say I understand the documentation really, and anyway, the nifty stuff seems to be >= 2.6 whereas I need to support Python 2.5 for a little while still …)

  • 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-11T23:00:02+00:00Added an answer on June 11, 2026 at 11:00 pm

    Yes – this solution is quite close to as simple as you can get – and it is readable.

    You should go for it – the alternative is to learn about “Aspect Oriented” programing, and check available Python libraries for using aspect orientation – the use case for A.O. is
    more or less this: adding boiler plate code to all methods that have a common trait. (And in Python it is just a matter of using a proper module, no need for using an alternate compiler for a super-set of the language as happen with Java)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is motivated by something I've lately started to see a bit too
I started out with the Master-Detail application in XCode 4.2. It is my first
I've started out with a div box that contains a header within it. My
I just started out with Python and wanted to try out tornado. Running the
I just started out with jQuery and really only need it for some very
We have just started out with Team Foundation Server 2008 / Visual Studio Team
I have just started out with testing some php mvc framework In it, it
I just started out with a new site and I decided to use DOMImplementation
I just started out with C and have very little knowledge about performance issues
I just started out using wordpress as a CMS. On the site I have

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.