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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:59:05+00:00 2026-06-15T19:59:05+00:00

Using default arguments of the form x={} usually does not accomplish the intended purpose

  • 0

Using default arguments of the form x={} usually does not accomplish the intended purpose in Python, since default arguments are bound when a function is defined, not called.

The convention seems to be to set mutable objects as default arguments with x=None and then check x is None to assign the proper default when the function is called.

So if I would like to cast x to a dictionary that is by default empty, I would use something like the following:

def f(x=None):
    x = dict(x) if x is not None else {}

However, since dict can take any iterable, I could also write this more succinct version:

def f(x=()):
    x = dict(x)

Which of these is the “right” approach?

  • 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-15T19:59:06+00:00Added an answer on June 15, 2026 at 7:59 pm

    The idiomatic style is to not cast to dict; that way someone could use any object that implements the correct mapping methods.

    So, the most pythonic method is to use:

    def f(x=None):
        if x is None:
            x = {}
    

    and then just use mapping methods.

    So, you generally should not cast arguments to a dict. You state your API accepts a mapping object instead, and expect callers to do the casting.

    The only reason to accept both a dict and an iterable is when you want to support ordered key-value pairs where duplicate keys are allowed, such as for the urllib.urlencode function. In such a case a dict cannot retain that information, and that method does not cast the iterable to a dict, but rather uses the dict as an iterable.

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

Sidebar

Related Questions

I am using python default development http server, and it works fine. Error ,The
I'm not using the default Visual Studio project path to build my program into,
I have taken to using setters instead of putting arguments into the default constructor
How does one set a default value for a text input using ActiveScaffold 1.2RC1?
I have webserver using default virtualhost apache <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www <Directory
using the default route routes.MapRoute( Admin, // Route name Admin/{action}, // URL with parameters
Even using the default test code on my app I can't get this working.
im using a Default.png file for my iPad-app. It appears correctly but i could'nt
I'm using the default Virtuemart product search module. If I search for something, I'm
I am using their default POS tagging and default tokenization..and it seems sufficient. I'd

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.