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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:20:37+00:00 2026-05-15T06:20:37+00:00

I want to write functions which are lazy as well as chainable. What would

  • 0

I want to write functions which are lazy as well as chainable. What would be the best way.
I know that one way would be to do yield instead of return.

I want these functions to be lazy in the way similar to how sqlalchemy functions are lazy when asked to fetch the data from DB.

  • 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-15T06:20:37+00:00Added an answer on May 15, 2026 at 6:20 am

    Generators (functions with yield instead of return) can indeed be seen as “lazy” (and itertools.chain can chain them just as well as any other iterator, if that’s what you mean by “chainable”).

    But if by “chainable” (and lazy) you mean you want to call fee().fie().fo().fum() and have all the “hard work” happen only in fum (which seems closer to what SQLAlchemy does), then generators won’t help — what you need, rather, is the “Promise” design pattern, where each function/method (except the one actually doing all the work) returns an object which records all the conditions, parameters, and constraints on the operation, and the one hard-working function uses that information to finally perform the work.

    To give a very simple example, say that “the hard work” is performing an RPC call of the form remote(host, **kwargs). You could dress this up in “lazy chainable clothing” as follows:

    class RPC(object):
        def __init__(self, host):
            self._host = host
            self._kws = {}
        def doit(self, **morekws):
            return remote(self._host, **dict(self._kws, **morekws))
        def __getattr__(self, name):
            def setkw(value):
                self._kws[name] = value
                return self
            return setkw
    

    Now, RPC(x).foo('bar').baz('bap').doit() calls remote(x, foo=bar, baz=bap) (and of course you can save intermediate stages of the chain, pass them around as arguments, etc, etc, until the call to doit).

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

Sidebar

Related Questions

i want to write a function that prints multi-dimensional objects which are text (or
For example, I want to write overload functions for set_difference which compares the type
I want to write a test case which will test a list of functions.
I want to write a function which can validate a given value (passed as
I want to write a function like equalp, which gives #t for (equalp Xy
I want to write a function in PL/SQL, which has a sql-statement as parameter.
i want to create functions what get the folder size. I write small function:
I want to write 'twice' function that takes a function and an argument and
I want to write a function that read line by line from a socket
I have problem with fancybox. I want to write a function that will run

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.