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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:37:42+00:00 2026-06-04T04:37:42+00:00

I have several class where I need to inject a static method; this static

  • 0

I have several class where I need to inject a static method; this static method should be called with type (not instance) as the first argument, and pass all remaining args to the implementation (the example at ideone):

# function which takes class type as the first argument
# it will be injected as static method to classes below
def _AnyClass_me(Class,*args,**kw):
    print Class,str(args),str(kw)

 # a number of classes
class Class1: pass
class Class2: pass

# iterate over class where should be the method injected
# c is bound via default arg (lambda in loop)
# all arguments to the static method should be passed to _AnyClass_me
# via *args and **kw (which is the problem, see below)
for c in (Class1,Class2):
    c.me=staticmethod(lambda Class=c,*args,**kw:_AnyClass_me(Class,*args,**kw))

# these are OK
Class1.me()    # work on class itself
Class2().me()  # works on instance as well

# fails to pass the first (Class) arg to _anyClass_me correctly
# the 123 is passed as the first arg instead, and Class not at all
Class1.me(123)
Class2().me(123)

The output is (first two lines correct, other two incorrect):

__main__.Class1 () {}
__main__.Class2 () {}
123 () {}
123 () {}

I suspect there is a problem in the lambda line, in the mixture of default argument with *args but I am unable to sort it out.

How can I have the Class object being passed correctly in presence of other args?

  • 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-04T04:37:43+00:00Added an answer on June 4, 2026 at 4:37 am

    You need to use a class method instead of a static method.

    for c in (Class1,Class2):
        c.me = classmethod(_AnyClass_me)
    
    >>> Class1.me()
    __main__.Class1 () {}
    >>> Class2().me()
    __main__.Class2 () {}
    >>> Class1.me(123)
    __main__.Class1 (123,) {}
    >>> Class2().me(123)
    __main__.Class2 (123,) {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JUnit test class in which I have several static final int
following setup, i have several generic functions, and i need to choose the type
I need to have several instances of a union as class variables, so how
I have a method in a singleton class that need to use the .NET
I have an Object base class, and I have several derived classes called Item,
I have several properties with getter and setter method in one action class. Those
I have several classes that do not really need any state. From the organizational
I have a winform with a form called MainForm. I have a static class
I have several divs with class image and several with class thumbnail (both can
I have several of the same class of element on a page and am

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.