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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:42:11+00:00 2026-06-14T00:42:11+00:00

Say I have a function or method that does something repetitive, like checking a

  • 0

Say I have a function or method that does something repetitive, like checking a value, before performing every operation it does, like so:

def myfunc():
    if mybool:
        do_operation_1()
    else:
        return

    if mybool:
        do_operation_2()
    else:
        return

    ...

These checks get repetitive, and end up wasting a lot of time and keyboard springs, especially when they are needed very often.

If you have control over the operation functions, like, do_operation_N you can decorate the functions with something that checks the boolean.

But what if you don’t have control over the individual do_operation_N operations? If, for each line in a function or method, I want the same check to be performed, is there some way to “insert” it without explicitly writing it in on each operation line? For example, is there some decorator magic by which I could do the following?

def magic_decorator(to_decorate):
    def check(*args, **kwargs):
        for call in to_decorate: #magic
            if mybool:
                to_decorate.do_call(call) #magic
            else:
                return #or break, raise an exception, etc
    return check

@magic_decorator
def myfunc():
    do_operation_1()
    do_operation_2()
    ...

If there is a way to achieve this, I don’t care if it uses decorators or not; I just want some way to say “for every line in function/method X, do Y first”.

The “magic” example of a do_call method above is shorthand for what I’m after, but it would encounter serious problems with out-of-order execution of individual lines (for example, if a function’s first line was a variable assignment, and its second was a use of that variable, executing them out of order would cause problems).

To be clear: the ability to externally control the line-by-line order of a function’s execution is not what I’m trying to achieve: ideally, I’d just implement something that, in the natural execution order, would perform an operation each time myfunc does something. If “does something” ends up being limited to “calls a function or method” (excluding assignments, if checks, etc), that is fine.

  • 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-14T00:42:12+00:00Added an answer on June 14, 2026 at 12:42 am

    Store your operations in a sequence, then use a loop:

    ops = (do_operation_1, do_operation_2, do_operation_3)
    
    for op in ops:
        if mybool:
            op()
        else:
            return
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that does a time consuming operation, say something like ten
Say I have a function that does the following in Vb.net For i as
let say i have function like below function doSomethingNow(){ callSomethingInFutureNotExistNow(); } at the moment
Say I have a function func(i) that creates an object for an integer i,
Say I have a function foo that I want to call n times. In
Say I have a function that takes an arbitrary number of arguments (the last
Lets say I have a function where the parameter is passed by value instead
So I have a a text file that looks something like this: public >ret
So I'm trying to use the .hover() method that only does something if the
Say i have this function that dynamically creates my namespace for me when I

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.