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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:35:59+00:00 2026-05-28T05:35:59+00:00

I have written a chainable API (in a similar style to jQuery) as follows:

  • 0

I have written a chainable API (in a similar style to jQuery) as follows:

class ChainableAPI:
  def show(self):
    self.visibility = 1
    return self

  def goBlue(self):
    self.color = 'Blue'
    return self

  def die(self):
    self.life = 0
    return self

chainableAPI = ChainableAPI()

(chainableAPI
  .goBlue()
  .show()
  .die()
  .goBlue())

Notice how each method of ChainableAPI ends with return self. Is there a way to have all methods to return self automatically? That way I do not have to specify return self manually for each method.

  • 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-28T05:36:00+00:00Added an answer on May 28, 2026 at 5:36 am

    You could use a decorator:

    import functools
    
    def chainable(func):
        @functools.wraps(func)
        def decorated(*args, **kwargs):
            func(*args, **kwargs)
            return args[0]
        return decorated
    

    Then the class would look like this:

    class Foo(object):
        @chainable
        def show(self):
            self.visibility = True
    
        @chainable
        def go_blue(self):
            self.color = 'blue'
    

    But is it better than just writing return self?

    Also, a class decorator could be written to apply the chainable decorator to all methods not starting with _:

    def all_chainable(klass):
        for k, v in klass.__dict__.items():
            if not k.startswith('_') and callable(v):
                setattr(klass, k, chainable(v))
        return klass
    

    Example:

    @all_chainable
    class Foo(object):
        def show(self):
            self.visibility = True
    

    Still, no matter how cool it may be I still think you should just use return self. But this shows how powerfull Python is.

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

Sidebar

Related Questions

have written this little class, which generates a UUID every time an object of
Have written all the code in a silverlight class library (dll) and linked this
I have written a generic class class MyClass : MyClass<string> { } //this line
Have written Geolocation API code for android and iphone. Take reference from http://dev.w3.org/geo/api/spec-source.html .
I have written a small jQuery script that checks the computed size of a
I have written a site in Prototype but want to switch to jQuery. Any
I have written a Scala class Sheltie to understand how scopes work in Scala.
I have written this little jquery, which has a nice hover effect. However, on
I've written a class that implements IEnumerable<T> . I have a method that returns
i have written the below code to display the AlertDialogBox as follows private void

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.