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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:46:52+00:00 2026-06-06T16:46:52+00:00

I’m writing an api and was wondering what’s the most pythonic way to do

  • 0

I’m writing an api and was wondering what’s the most pythonic way to do the following.

I’m writing a bunch of methods to do various web calls, the arguments mostly translate into post data keys and values.

The way I’ve been writing it so far is mostly like this;

def doSomething(self,param1,param2,param3):
    payload={"param1":param1,
             "param2":param2,
             "param3":param3}
    return self.request("do/something",payload)

This already has the draw back of having to repeat the parameter names which are subject to change, but this pattern isn’t too bad.

The following case is what got me trying to think of a better way. In this case there are 4 optional arguments for the call

def doSomethingElse(self,param1,param2=None,param3=None,param4=None,param5=None):
    payload= {"param1":param1}
    if param2:
        payload["param2"]= param2
    if param3:
        payload["param3"]= param3
    # ... etc ...
    self.request("do/something/else",payload)

My first thought was to do this:

def doSomethingElse(self,param1,**params):
    payload = {"param1":param1}
    payload.update(params)
    self.request("do/something/else",payload)

or even:

def doSomethingElse(self,**payload):
    self.request("do/something/else",payload)

Although the second one is nice and simple, the method can be called without the non-default argument. But in both cases I lose the method signature when using the api and the user won’t know what the parameters are (I know I could write the expected signature in a docstring but I’d rather prevent misspelt keywords getting sent).

I’m thinking there must be a nice pythonic solution to do this, any ideas?

EDIT

I think a key point which I didn’t make clear enough is that the arguments are getting sent in post data in a call, and I want to make sure only those keys can get sent, like in the first example of doSomethingElse, you can’t send anything other than those 5 named parameters.

  • 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-06-06T16:46:53+00:00Added an answer on June 6, 2026 at 4:46 pm

    How about simply

    def get_payload(ldict):
        return {k:v for k,v in ldict.iteritems() if k != 'self' and v is not None}
    
    class fred(object):
        some_class_var = 17
        def method(self, a, b=2):
            payload = get_payload(locals())
            print payload
    

    which gives

    >>> f = fred()
    >>> f.method()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: method() takes at least 2 arguments (1 given)
    >>> f.method(2)
    {'a': 2, 'b': 2}
    >>> f.method(2, b=3)
    {'a': 2, 'b': 3}
    >>> f.method(5, b=None)
    {'a': 5}
    >>> f.method(2, b=3, c=19)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: method() got an unexpected keyword argument 'c'
    >>> help(f.method)
    
    Help on method method in module __main__:
    
    method(self, a, b=2) method of __main__.fred instance
    

    which I think matches your criteria. The next step would be to use a decorator (probably with either wraps or the decorator module to preserve the signature) so that payload was computed and then passed, but I don’t know if @payload would be all that much better than payload = get_payload(locals()). Note that using locals() this way, it needs to be done at the start.

    I second the feeling that this isn’t exactly the best way to prevent unwanted nuclear attacks, though.

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

Sidebar

Related Questions

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 have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am writing an app with both english and french support. The app requests
I have a reasonable size flat file database of text documents mostly saved in
I'm parsing an XML file, the creators of it stuck in a bunch social

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.