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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:15:07+00:00 2026-05-26T13:15:07+00:00

Imagine this: def b1(fnc): print b1 return fnc @b1 def a1(): print a1 if

  • 0

Imagine this:

def b1(fnc):
  print "b1"
  return fnc

@b1
def a1():
  print "a1"

if __name__ == "__main__":
  a1() # will print b1 a1

So, when I’m using @b1, a1 gets turned to a1 = b1(a1), right?
Then, when I say:

a1()

This turns to:

b1(a1)

And then goes into:

print "b1"
return fnc

Where/Who exactly is calling fnc? I have a feeling I’m asking a very stupid question, but I want to understand.

  • 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-26T13:15:08+00:00Added an answer on May 26, 2026 at 1:15 pm

    The decorator is only executed once. It takes a callable object and returns a callable object. The object that it returns is used in place of a1.

    In other words, b1 is called at the point where a1 is defined. It prints out "b1" and returns a1 unchanged. Since it returns a1 unchanged, b1 plays no role whatsoever in any subsequent calls to a1.

    Therefore, the following comment isn’t quite correct:

      a1() # will print b1 a1
    

    In fact,

    • a1() only prints "a1".
    • The "b1" that you see is printed by @b1 / def a1():.

    If you change the code like so and re-run, the sequence of events should become clearer:

    def b1(fnc):
      print "b1"
      return fnc
    
    @b1
    def a1():
      print "a1"
    
    if __name__ == "__main__":
      print 'in __main__'
      a1()
      a1()
    

    Finally, to achieve the effect you were looking for, the decorator would need to return a different callable object:

    def b1(fnc):
      def decorated():
        print "b1"
        return fnc()
      return decorated
    
    @b1
    def a1():
      print "a1"
    
    if __name__ == "__main__":
      print 'in __main__'
      a1()
      a1()
    

    Here, it is pretty clear who is calling fnc().

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

Sidebar

Related Questions

Imagine this: def method(self, alpha, beta, gamma, delta, epsilon, zeta, eta, theta, iota, kappa):
Imagine this controller: class exampleController{ def action1 = {} def action2 = {} def
Imagine I have something like this: def example = { def temp = ConferenceUser.findAllByUser(User.get(session.user))
Imagine this: class A(object): class B(object): def __init__(self): super(B, self).__init__() This creates an error:
in views.py I have this: def logout(request,key): auth.logout(request) return HttpResponseRedirect(reverse('airAgency.views.index',args=[key])) in index template i
Imagine I have this scenario in my Controller: def nr_1 = params.first_nr def nr_2
Imagine you have a map like this: (def person { :name { :first-name John
Imagine I have a test like this: class MyUnitTest < Test::Unit::TestCase def test_first #
Anyone can help with this? Imagine the following: def example(s): s = s.replace('foo', 'foo
Imagine this code: class Foo { println(in Foo) def foo(a: Int) = a +

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.