0

我有:

Class A:
    def __init__(self, y):
        blah, blah, blah
    def af(self, h):
        print "this"

我像这样骑过defs:

def my_init(self,h):
    gangsta wangsta
def aff(self,h):
    print "that"

A.af = aff # works
A.__init__ = my_init # doesn't work

它不适用于 init....我该怎么做__init__

4

2 回答 2

2

最简单的方法是子类化:

class B(A):
     __init__ = my_init
于 2013-02-25T19:55:14.797 回答
2

究竟是什么问题?

>>> class X:
...   def __init__(self):
...     print "Original Init!"
...
>>> def new_init(self):
...    print "OK New Init"
>>> X()
Original Init!
>>> X.__init__ = new_init
>>> X()
OK New Init
于 2013-02-25T19:58:29.133 回答