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

The Archive Base Latest Questions

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

This is a class I have: class metadict(dict): def __init__(self, do_something=False, *args, **kwargs) if

  • 0

This is a class I have:

class metadict(dict):
    def __init__(self, do_something=False, *args, **kwargs)
        if do_something:
            pass
        super(metadict,self).__init__(*args,**kwargs)

The idea is to encapsulate a dictionary and add some functionality with a special keyword. The dictionary can still hold do_something though you can’t add it at creation time. For all other aspects it behaves just like a normal dictionary.

Anyway, the problem is that whatever I give to args it starts by assigning the first value to do_something which is not what I want.

What I do now is this:

class metadict(dict):
    def __init__(self, do_something=False, *args, **kwargs)
        if not isinstance(do_something, bool):
            args = list(args)
            args.append(do_something)
        elif do_something:
            pass
        super(metadict,self).__init__(*args,**kwargs)

But it doesn’t look right to me. I could also check for the do_something value in kwargs, but it will be worse, since I mess with the signature removing useful information…

Is there any way in python to use optional, positional and keyword arguments safely?
If not are there other simpler workarounds?

I’m on python 2.6

  • 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-15T09:43:00+00:00Added an answer on June 15, 2026 at 9:43 am

    It’s new in Python 3. The best workaround in Python 2 is

    def foo(*args, **kwargs):
        do_something = kwargs.pop("do_something", False)
    

    The behaviour you see happens because Python tries to be clever in matching up arguments, so for instance it will make a keyword argument positional if you pass too many positional arguments.

    PS why not store it as an attribute of metadict instead of as an entry in the dict?

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

Sidebar

Related Questions

I have class: @custom_decorator class Creature(objects): def __init__(self): pass def rise_hands(self, *args, **kwargs): print
Suppose I have this class: class MyClass(object): def uiFunc(self, MainWindow): self.attr1 = foo self.attr2
i make one class file for jar. and this class have use wurfl. and
I have class that looks like this: class A { public: class variables_map vm
I have this class that have a function to load other classes and create
I have a Class(ChorNumbers) and in this class i have a Function that test
I copied this class from a similar class I have in this app that
I have class A which extends the Activity class. This class is in package
HI All. I have subclass of UIScrollView. IN this class I have added some
So I have this class which extends an activity. But I want to draw

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.