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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:17:10+00:00 2026-06-17T07:17:10+00:00

I don’t have much experience in python but I am studying **kwargs . Afer

  • 0

I don’t have much experience in python but I am studying **kwargs.

Afer reading a lot I understood somethings about **kwargs but I have a small problem or I am not understanding something correct.

So this works:

def test_var_kwargs(farg, **kwargs):
    print "formal arg:", farg
    for key in kwargs:
        print "another keyword arg: %s: %s" % (key, kwargs[key])

test_var_kwargs(farg=1, myarg2="two", myarg3=3)

And prints:

formal arg: 1

another keyword arg: myarg2: two

another keyword arg: myarg3: 3

But if that function was an instance function then self would have to be included:

def test_var_kwargs(self, farg, **kwargs):
        print "formal arg:", farg
        for key in kwargs:
            print "another keyword arg: %s: %s" % (key, kwargs[key])
    
self.test_var_kwargs(farg=1, myarg2="two", myarg3=3)

But this produces an error:

TypeError: test_var_kwargs() takes exactly 2 arguments (1 given)

I understand that I have to pass self like:

self.test_var_kwargs(self, farg=1, myarg2="two", myarg3=3)

Why do I have to include self as an argument in the class instance’s method?

  • 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-17T07:17:11+00:00Added an answer on June 17, 2026 at 7:17 am

    You cannot use farg as keyword argument in that case; it cannot be interpreted as both a positional and a keyword argument, Python interprets it as a keyword argument in this case.

    Use

    self.test_var_kwargs(self, 1, myarg2="two", myarg3=3)
    

    instead.

    Functions act as descriptors; when looked up on an instance, they get wrapped in a new object called a method, which automatically adds the instance as a first argument to the function when called. This wrapper essentially does this:

    class Method(object):
        def __init__(self, function, instance, cls):
            self.func = function
            self.instance = instance
            self.cls = cls
    
        def __call__(self, *args, **kw):
            return self.func(self.instance, *args, **kw)
    

    Your farg keyword argument is then lumped under the **kw catchall and passed on to the underlying function, with *args left empty.

    But the test_var_kwargs method you defined has one positional argument (next to self, and thus an exception is raised.

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

Sidebar

Related Questions

I don't have much knowledge about the IPv6 protocol, so sorry if the question
don't know if the title describes anything about what I'm trying to say but
Don't ask me how but I'm in a situation where I have DCPs published
Don't ask why but I have the requirement to draw a border around certain
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't know a whole lot about streams. Why does the first version work using
Don't know if this is possible, but I have some code like this: val
Don't know if it matters, but the msg.d is about 300 rows long. I
Don't get me wrong PDO is great but what I don't like about it,
Don't know much about encryption... Say I'm preparing a SAML request to submit to

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.