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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:07:01+00:00 2026-05-14T03:07:01+00:00

I guess the subject sounds pretty stupid, so I’ll show some code: def foo(**kwargs):

  • 0

I guess the subject sounds pretty stupid, so I’ll show some code:

def foo(**kwargs):
    # How can you detect the difference between (**{}) and ()?
    pass
foo(**{})
foo()

Is there any way to detect inside of foo, how the method was called?

Update 1

Because there were some comments why you possible want to do something, I’ll try to explain some background.

super(MyClass, self).foo(*args, **kwargs) sucks – a lot of wasteful duplication. I want to write ‘self.super()’. In this case, just call the super class and hand over all parameters that the calling method got as well. Works like a charm.

Now the problematic magic part:

I want to say ‘self.super(something)’ and in this case, only ‘something’ is passed to the super method. Works for most cases.

This is where it breaks:

def foo(self, fnord=42, *args, **kwargs):
    self.super(*args, **kwargs)

So the super method should get the arguments that the calling method – however if *args, **kwargs are empty, currently the library can not detect this condition and passed all arguments including ‘fnord’…

Of course I could use self.super.foo(*args, **kwargs) as an alternative syntax but that’s lame 🙂

PS: Yes, I know p3k’s super, but still not nice and it does not work with Python 2.x…

Update 2

Actually even Python’s ast module removes the **{} (ast.parse('foo(**{})')) so it looks like this happens so early in the parsing process that you can not get this information later on…

So in the end I have either to give up on that specific problem (raising an AmbiguousSyntaxError) or to use text parsing as proposed by ~unutbu. Re-thinking my approach, the latter might actually feasable because I only need to know if it is self.super(\s*), self.super(\S+).

  • 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-14T03:07:01+00:00Added an answer on May 14, 2026 at 3:07 am

    This hack only works with CPython.

    import traceback
    
    def foo(**kwargs):
        # stack is a list of 4-tuples: (filename, line number, function name, text)
        # see http://docs.python.org/library/traceback.html#module-traceback
    
        (filename,line_number,function_name,text)=traceback.extract_stack()[-2]
        print('foo was called: %s'%text)
    
    foo(**{})
    # foo was called: foo(**{})
    foo()
    # foo was called: foo()
    

    As an example of how this might be useful:

    def pv(var):
        (filename,line_number,function_name,text)=traceback.extract_stack()[-2]
        print('%s: %s'%(text[text.find('(')+1:-1],var))
    
    x=5
    pv(x)
    # x: 5
    

    Notice that pv is called with just the value x, but
    it prints both the “name” of the variable (as it was called), and the value of a variable. Sometimes I use this when debugging and am too lazy to write out the full print statement.

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

Sidebar

Related Questions

Guess that I have a TextView that I want to update it in some
I guess I am looking for some kind of intro and see if anybody
I guess it is first time I see Yahoo mail doing some thing with
My previous question about this subject was answered and I got some tests working
It seems this subject is recurring, but sorry, I can't really find any answer
So in my current code i'm working on some sort of notification manager. The
I have looked at some of the related posts on this subject but i
I need to show TM symbol in the subject of an email. I am
I guess that the difficult part is the source. Do you know any webpage
I guess getView() is called for each item of the list, but my question

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.