-1

私はこの方法を持っています:

def get_compression_settings(self, media_type=None):
        return self.transmit('GET', 'compression?mediatypeid={0}'.format(media_type))

media_type == None かどうかを確認し、三項演算子を使用して 1 行で ~=None の場合のみ mediatypeid={0} を追加する方法はありますか?

私は次のことができることを知っていますが、私のメソッドが単一の戻り値になればもっと良いでしょう:

def get_compression_settings(self, media_type=None):
        endpoint = 'compression?mediatypeid={0}'.format(media_type) if media_type is not None else 'compression'
        return self.transmit('GET', endpoint)
4

2 に答える 2

3

次のように、単一の改行にすることができます。

return self.transmit('GET', 'compression?mediatypeid={0}'.format(media_type)
                             if media_type is not None else 'compression')

読みやすくするために複数の行に分割しましたが、それは機能するために必要ではありません。if media_type not none(これはあなたの例で に変更する必要があることに注意してくださいif media_type is not None)。

于 2013-01-14T17:54:55.733 に答える
1

これを試しましたか?

return self.transmit('GET', 'compression?mediatypeid={0}'.format(media_type) if media_type is not None else 'compression')
于 2013-01-14T17:55:37.027 に答える