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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:30:48+00:00 2026-05-28T14:30:48+00:00

Python doesn’t support complicated anonymous functions. What’s a good alternative? For example: class Calculation:

  • 0

Python doesn’t support complicated anonymous functions. What’s a good alternative? For example:

class Calculation:
    def __init__(self, func):
        self.func = func

    def __call__(self, data):
        try:
        # check if the value has already been calculated
        # if it has, it would be cached under key = self.func
            return data[self.func]
        except KeyError:
            pass # first-time call; calculate and cache the values
        data[self.func] = self.func(data)
        return data[self.func]

# with a simple function, which can be represented using lambda, this works great
f1 = Calculation(lambda data : data['a'] * data['b'])

# with a complicated function, I can do this:
def f2_aux:
   # some complicated calculation, which isn't suitable for a lambda one-liner
f2 = Calculation(f2_aux) 

Is this a reasonable design to begin with?

If so, is there a way to avoid the ugliness of f*_aux for each f* that I define in the module?

UPDATE:

Example of use:

d = {'a' : 3, 'b' : 6}

# computes 3 * 6
# stores 18 in d under a key <function <lambda> at ...>
# returns 18
f1(d)

# retrieves 18 from d[<function <lambda> at ...>]
# returns 18, without having to recalculate it
f1(d)

UPDATE:

Just for my understanding, I added a version that uses the inner function.

def memoize(func):
    def new_func(data):
        try:
        # check if the value has already been calculated
        # if it has, it would be cached under key = self.func
            return data[func]
        except KeyError:
            pass # first-time call; calculate and cache the values
        data[func] = func(data)
        return data[func]
    return new_func

@memoize
def f1(data):
  return data['a'] * data['b']
  • 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-28T14:30:49+00:00Added an answer on May 28, 2026 at 2:30 pm

    You don’t need anonymous functions. Also, memoization has been done better than this, there’s probably no reason for you to roll your own.

    But to answer the question: You can use your class as a decorator.

    @Calculation
    def f2():
        ...
    

    This simply defined the function, wraps it in Calculation and stored the result of that as f2.
    The decorator syntax is defined to be equivalent to:

    _decorator = Calculation # a fresh identifier
    # not needed here, but in other cases (think properties) it's useful
    def f2():
        ...
    f2 = _decorator(f2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why doesn't the following work (Python 2.5.2)? >>> import datetime >>> class D(datetime.date): def
class Package: def __init__(self): self.files = [] # ... def __del__(self): for file in
class Phone: def __init__(self): self.types = [Touch Screen,Flip, Slider, Bar, Bag] self.brand = No
Python Decimal doesn't support being constructed from float; it expects that you have to
I'd love a good native Python library to write XLS, but it doesn't seem
I know that Python doesn't support tail-call optimization. Does that mean a recursive procedure
Python doesn't allow non-hashable objects to be used as keys in other dictionaries. As
Am I correct in thinking that that Python doesn't have a direct equivalent for
Why doesn't Python allow modules to have a __call__ method? (Beyond the obvious that
It appears the Python signal module doesn't have anything similar to the sighold and

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.