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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:40:46+00:00 2026-06-15T19:40:46+00:00

Possible Duplicate: Understanding Python decorators Just trying to port some Python code to Java,

  • 0

Possible Duplicate:
Understanding Python decorators

Just trying to “port” some Python code to Java, I came then across the following python code:

 @fake(lambda s, t, n: [(s.field(i+1), s) for i in range(n)])
 def split(secret, threshold, num_players):
     shares = []
     for i in range(1, num_players+1):
         # do some shares calculation
     return shares

There are quite some interesting constructs in this one that I never noticed before. Could anyone tell me what is the deal with this @fake thingy?

def fake(replacement):
    """Replace a function with a fake version."""
    def decorator(func):
        fakes = os.environ.get('FUNC_FAKE', '')
        if fakes == '*' or func.__name__ in fakes.split():
            return replacement
        else:
            return func
    return decorator

Further, does this lambda stand for a function name or what is the deal with that?

  • 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-15T19:40:47+00:00Added an answer on June 15, 2026 at 7:40 pm

    First of all, @fake is a decorator.

    What @fake appears to do is to conditionally replace the function that follows, i.e. split, with the lambda function (note how the two take the same parameters).

    The decision is based on the FUNC_FAKE environment variable. If the latter equals * or contains split as one of its tokens, the replacement is made. Otherwise, it isn’t.

    The fact that the replacement is a lambda function is not important. It could have just as easily been made into a normal function:

    def split_replacement(s, t, n):
       return [(s.field(i+1), s) for i in range(n)])
    
    @fake(split_replacement)
    def split(s, t, n):
       ...
    

    This whole construct is rather baffling. I struggle to come up with a reason for doing things this way, other than to try and confuse other programmers (or to play with decorators).

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

Sidebar

Related Questions

Possible Duplicate: Understanding Python decorators I was reading a django app source code where
Possible Duplicate: Understanding Python decorators Could you please give a short code example that
Possible Duplicate: Understanding Python's call-by-object style of passing function arguments I recently came across
Possible Duplicate: Understanding Python decorators What does a Python decorator do? And where can
Possible Duplicate: Understanding pthread_detach The following code is creating a single thread which prints
Possible Duplicate: What does *args and **kwargs mean? Understanding kwargs in Python I've found
Possible Duplicate: Understanding NSString comparison in Objective-C I've used this simple code for several
Possible Duplicate: Understanding NSString comparison in Objective-C Was just reading up about equality vs
Possible Duplicate: Understanding Enums in Java Why should we use enums rather Java constants?
Possible Duplicate: Undefined, unspecified and implementation-defined behavior I'm trying to deepen my understanding of

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.