fork download
  1. # function which takes class type as the first argument
  2. # it will be injected as static method to classes below
  3. def _AnyClass_me(Class,*args,**kw):
  4. print Class,str(args),str(kw)
  5.  
  6. # a number of classes
  7. class Class1: pass
  8. class Class2: pass
  9.  
  10. # iterate over class where should be the method injected
  11. # c is bound via default arg (lambda in loop)
  12. # all arguments to the static method should be passed to _AnyClass_me
  13. # via *args and **kw (which is the problem, see below)
  14. for c in (Class1,Class2):
  15. c.me=staticmethod(lambda Class=c,*args,**kw:_AnyClass_me(Class,*args,**kw))
  16.  
  17. # these are OK
  18. Class1.me() # work on class itself
  19. Class2().me() # works on instance as well
  20.  
  21. # fails to pass the first (Class) arg to _anyClass_me correctly
  22. # the 123 is passed as the first arg instead, and Class not at all
  23. Class1.me(123)
  24. Class2().me(123)
  25.  
Success #stdin #stdout 0.02s 4676KB
stdin
Standard input is empty
stdout
__main__.Class1 () {}
__main__.Class2 () {}
123 () {}
123 () {}