How much sanity checking is required for function inputs?

Christopher Reimer christopher_reimer at icloud.com
Thu Apr 21 22:55:18 EDT 2016


On 4/21/2016 6:54 PM, Tim Chase wrote:
>
> I'd simplify this code to something like
>
>    class PieceFactory(object):
>      @staticmethod
>      def factory(color, piece, position):
>        try:
>          return {
>            'Bishop': Bishop,
>            'King': King,
>            'Knight': Knight,
>            'Pawn': Pawn,
>            'Queen': Queen,
>            'Rook': Rook,
>            }[piece](color, position)
>        except KeyError:
>          raise PieceException(...)

I like this better. I'll probably remove the try/except and let the 
KeyError filter up.

Thanks,

Chris R.



More information about the Python-list mailing list