在python中覆盖类__init__的方法

时间:2015-03-09 19:24:50

标签: python override init

当我调用mic.openTheMic(record_seconds = 10)时,我想要使用所有类默认值,或者如果我想更改(一个或多个)参数,那么只需要包含一个,例如,这种情况record_seconds = 10。有没有办法做到这一点?

class myAudio(object):
    def __init__(self, chunk = 1024, format = pyaudio.paInt16, channels = 1,
        rate = 41000, record_seconds = 5):
        self.p = pyaudio.PyAudio()
        self.CHUNK = chunk
        self.FORMAT = format
        self.CHANNELS = channels
        self.RATE = rate
        self.RECORD_SECONDS = record_seconds


    def openTheMic(chunk = 1024, format = pyaudio.paInt16, channels = 1,
            rate = 41000, record_seconds = 5):

        q = myAudio(chunk = 1024, format = pyaudio.paInt16, channels = 1,
            rate = 41000, record_seconds = 5)

        q = myAudio()

        stream = q.p.open(format = q.FORMAT,
        channels = q.CHANNELS,
        rate = q.RATE,
        input = True,
        output = True,
        frames_per_buffer = q.CHUNK)

        print ("***mic is open***")

        for i in range(0, q.RATE / q.CHUNK * q.RECORD_SECONDS):
             data = stream.read(q.CHUNK)

if __name__ == '__main__':
    mic = myAudio()
    mic.openTheMic(record_seconds = 10)

1 个答案:

答案 0 :(得分:0)

如何(未经测试):

class myAudio(object):
    _map = {
        'chunk': 'CHUNK',
        'fmt': 'FORMAT',
        'channels': 'CHANNELS',
        'rate': 'RATE',
        'record_seconds': 'RECORD_SECONDS',
    }

    def __init__(self, chunk=1024, fmt=pyaudio.paInt16, channels=1, rate=41000, record_seconds=5):
        self.p = pyaudio.PyAudio()
        self.CHUNK = chunk
        self.FORMAT = fmt
        self.CHANNELS = channels
        self.RATE = rate
        self.RECORD_SECONDS = record_seconds


    def openTheMic(self, **kwargs):
        for (k,v) in kwargs.iteritems():
            setattr(self, myAudio.__map[k], v)

        stream = self.p.open(
            format = self.FORMAT,
            channels = self.CHANNELS,
            rate = self.RATE,
            input = True,
            output = True,
            frames_per_buffer = self.CHUNK
        )

        print ("***mic is open***")

        for i in range(0, self.RATE / q.CHUNK * self.RECORD_SECONDS):
             data = stream.read(self.CHUNK)

if __name__ == '__main__':
    mic = myAudio()
    mic.openTheMic(record_seconds = 10)

请注意,我已将format更改为fmt,以避免影响内置内容。此外,对openTheMic的调用会永久更改实例属性值 。如果你不想要那个,可能是这样的:

class myAudio(object):

    def __init__(self, chunk=1024, fmt=pyaudio.paInt16, channels=1, rate=41000, record_seconds=5):
        self.p = pyaudio.PyAudio()
        self.CHUNK = chunk
        self.FORMAT = fmt
        self.CHANNELS = channels
        self.RATE = rate
        self.RECORD_SECONDS = record_seconds


    def openTheMic(self, **kwargs):
        _chunk          = kwargs.get('chunk',          self.CHUNK)
        _format         = kwargs.get('fmt',            self.FORMAT)
        _channels       = kwargs.get('channels',       self.CHANNELS)
        _rate           = kwargs.get('rate',           self.RATE)
        _record_seconds = kwargs.get('record_seconds', self.RECORD_SECONDS)

        stream = self.p.open(
            format            = _format,
            channels          = _channels,
            rate              = _rate,
            input             = True,
            output            = True,
            frames_per_buffer = _chunk
        )

        print ("***mic is open***")

        for i in range(0, _rate / _chunk * _record_seconds):
             data = stream.read(_chunk)

if __name__ == '__main__':
    mic = myAudio()
    mic.openTheMic(record_seconds = 10)