Does Python have something like anonymous inner classes of Java?

后端 未结 10 1281
一生所求
一生所求 2020-12-05 09:25

In Java you can define a new class inline using anonymous inner classes. This is useful when you need to rewrite only a single method of the class.

Suppose that you

10条回答
  •  忘掉有多难
    2020-12-05 09:55

    In python you have anonymous functions, declared using lambda statement. I do not like them very much - they are not so readable, and have limited functionality.

    However, what you are talking about may be implemented in python with a completely different approach:

    class a(object):
      def meth_a(self):
        print "a"
    
    def meth_b(obj):
      print "b"
    
    b = a()
    b.__class__.meth_a = meth_b
    

提交回复
热议问题