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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:34:33+00:00 2026-05-28T01:34:33+00:00

I’ve written a Python API that is chain based (similar to jQuery). So I

  • 0

I’ve written a Python API that is “chain based” (similar to jQuery). So I can write:

myObject.doStuff().doMoreStuf().goRed().goBlue().die()

The problem is that I haven’t found a way to keep the syntax clean with long chains. In JavaScript I could simply do

myOjbect
   .doStuf()
   .doMoreStuf()
   .goRed()
   .goBlue()
   .die()

but Python has indentation restrictions…

  • 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-28T01:34:33+00:00Added an answer on May 28, 2026 at 1:34 am

    PEP8-compliant solution: formatting the line

    Actually PEP8 says:

    Long lines can be
    broken over multiple lines by wrapping expressions in parentheses. These
    should be used in preference to using a backslash for line continuation
    .
    Make sure to indent the continued line appropriately.

    So I suppose your code should look like this:

    (
        myOjbect
        .doStuf()
        .doMoreStuf()
        .goRed()
        .goBlue()
        .die()
    )
    

    Alternative solutions: splitting into separate statements

    Judging from the syntax, there are two options possible regarding the values returned by each method call:

    1. Every method (maybe except die(), which is not required, as its result is not being used) returns modified instance (the same instance, on which it was called).
    2. Every method (still, die() is not required to do that) returns copy of the instance on which it was called.

    Solution for mutable objects (methods return original instance)

    In first case (when returning same instance), the solution to split longer lines into several statements is:

    myObject.doStuf()
    myObject.doMoreStuf()
    myObject.goRed()
    myObject.goBlue()
    myObject.die()
    

    Real world example involves mutable objects:

    my_stuff = []
    my_stuff.append('laptop')  # my_stuff == ['laptop']
    my_stuff.append('jacket')  # my_stuff == ['laptop', 'jacket']
    my_stuff.append('apple')  # my_stuff == ['laptop', 'jacket', 'apple']
    

    (although list.append() does not return anything, just for consistency and for stating explicitly that it is mutable)

    Solution for immutable objects (methods return modified copy)

    In the second case (when returning copy), the solution to do similar thing is:

    myObject = myObject.doStuf()
    myObject = myObject.doMoreStuf()
    myObject = myObject.goRed()
    myObject = myObject.goBlue()
    myObject.die()
    

    Real world example involves immutable objects:

    name = '-Tadek-'
    name = name.strip('-')  # name == 'Tadek'
    name = name.lower()  # name == 'tadek'
    name = name.replace('k', 'ck')  # name == 'tadeck'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

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.