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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:36:05+00:00 2026-05-23T13:36:05+00:00

I am designing the API for a Python library, and have run into a

  • 0

I am designing the API for a Python library, and have run into a situation where I think my imagination might have overtaken Python’s considerable abilities.

(I want to apologize to any Pythonistas who may get offended by the Ruby-ness of my design)

Basically, there is a class with a few methods on it, each of which returns self, so that they can be chained together. So:

>>> a = MyKlass()
>>> b = a.method_one()
>>> c = b.method_two()

would be the same as

>>> a = MyKlass()
>>> c = a.method_one().method_two()

My question is whether it is possible to make the parenthesis optional. I have learned a bit about using __getattr__ and __call__ to manipulate callable objects, but haven’t been able to fully implement this. Right now, I have the class’s __getattr__ checking for the attribute, and then determining whether the name is a method or attribute (or more accurately, whether it is callable or not).

My problem is that if it is a callable, I need __getattr__ to do something like this:

>>> class MyKlass(object):
>>>     def __getattr__(self, name):
>>>         # pseudocode, you get the idea...
>>>         if name is callable:
>>>             def callme(*args, **kwds):
>>>                 # do stuff
>>>             return callme
>>>         else:
>>>             # just return the attribute

So it returns a callable object. This means, though, that I couldn’t make the parenthesis optional, since the callable object would be returned, but never called:

>>> a = MyKlass()
>>> c = a.method_one.method_two    # AttributeError, since a bound function object does not have attribute "method_two"

The only way I thought of to do this would be to be able to “look ahead” in the code and determine whether the name is followed by parenthesis or not. Then, if name was callable but not called, I could return the results of calling name without arguments instead of returning a callable object.

I am thinking that this is probably not doable, but I thought that I would ask the gurus anyway.

  • 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-23T13:36:05+00:00Added an answer on May 23, 2026 at 1:36 pm

    I think you’re nuts for wanting to do this. What reason do you have? What about methods that take arguments? (See example below.)

    It seems to work for methods without arguments if you use __getattribute__ instead of __getattr__.

    Working example:

    class Test(object):
        def __getattribute__(self, name):
            attr = super(Test, self).__getattribute__(name)
            if callable(attr):
                return attr()
            return attr
        def f(self):
            print 'f'
            return self
        def g(self):
            print 'g'
            return self
        def h(self, x):
            print x
            return self
    
    t = Test()
    t.f.g # Works fine
    t.f().g # Fails - the parens aren't optional, they must be left off now
    t.f.g.h(1) # Fails - unable to provide an argument
    

    To get around the argument issue, you could inspect the function and only call it if it doesn’t take any arguments (other than self). See: http://docs.python.org/library/inspect.html

    Do yourself a favor and stop going down this path.

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

Sidebar

Related Questions

When designing an API, I may want to persist details (Eg of a process
I'm designing an API (in Java) and expect to have users accessing the API
I am designing an API for a C++ library which will be distributed in
I am designing an API. It will have a lot of methods which do
When designing user table what would be the must have fields from the security/user
In my new job more people are using Python than Perl, and I have
I'm designing an API to go over HTTP and I am wondering if using
I am designing an API, and some of the methods return null when they
I'm designing an API for a web service and I can't decide between using
I am designing RESTful Api's and would like some advice on designing an API

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.