1

For example:

class a:
    @staticmethod
    def aaa():
        print 'a'

a.aaa()
a.aaa = lambda: raise ValueError('a')
a.aaa()

The second time python raises an error that I didn't pass an instance of class a into the method. How can I change the implementation without removing the static property?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555

1 Answers1

0

Call the staticmethod decorator on it directly:

class a:
    @staticmethod
    def aaa():
        print 'a'

a.aaa()
# decorate the lambda expression with staticmethod
a.aaa = staticmethod(lambda: 'a')
a.aaa()

This is basically what you do when you use a decorator.

Note that you can not raise an error directly, since raise is a statement, not an expression. You can however use some tricks to raise an exception in a lambda expression.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555