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 7039751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:50:10+00:00 2026-05-28T01:50:10+00:00

My class function alway need do something before like this (Python): class X: def

  • 0

My class function alway need do something before like this (Python):

class X:
 def f1():
  #### 
  if check() == False: 
   return;
  set_A()
  ####
  f1_do_something

 def f2():
  #### 
  if check() == False: 
   return;
  set_A()
  ####

  f2_do_something

And I want to :

class X:
 def f1():
  # auto check-return-set_A()
  f1_do_something

 def f2():
  # auto check-return-set_A() 
  f2_do_something

I’ve read about design patterns and I do not know how to apply design patterns in this case. If there is no any suitable design patterns, are there any other solution to this problem?

  • 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-05-28T01:50:10+00:00Added an answer on May 28, 2026 at 1:50 am

    You could use a decorator:

    #!/usr/bin/env python
    
    def check(f, *args, **kwargs):
        def inner(*args, **kwargs):
            print 'checking...'
            return f(*args, **kwargs)
        return inner
    
    class Example(object):
    
        @check
        def hello(self):
            print 'inside hello'
    
        @check
        def hi(self):
            print 'inside hi'
    
    if __name__ == '__main__':
        example = Example()
        example.hello()
        example.hi()
    

    This snippet would print:

    checking...
    inside hello
    checking...
    inside hi
    

    If you go this route, please check out functools.wrap, which makes decorators aware of the original function’s name and documentation.

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

Sidebar

Related Questions

I need a function that will do something like this: $arr = array(); //
Is there a way to make a class function unoverriddable? Something like Java's final
Normally, I've seen prototype functions declared outside the class definition, like this: function Container(param)
I have a class: function RustEditor() { this.init = function() { var saveButton =
I need to call the co-class function by reading its address from vtable of
What does the following mean? Class.Function(variable := 1 + 1) What is this operator
I have a string that looks like this: Name1=Value1;Name2=Value2;Name3=Value3 Is there a built-in class/function
I have a base class that I want to look like this: class B
I really need to create something like the following, I am building 2 classes,
Just to clarify, I mean something like: class foon { private $barn = null;

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.