But I noticed how people also raised objections,
that checking for types betrays a "not object oriented" design
実はダックタイピングスタイル(「アヒルのように見えてアヒルのように鳴くなら、それはアヒルに違いない」)と呼ばれ、このスタイルのプログラミングを推奨しているのがpython言語です。
ダックタイピングでは、EAFP (許可よりも許しを求める方が簡単)と呼ばれるものが登場します。
presumable more "more object oriented" way of handling this without
explicitly checking for type?
あなたはよりpythonicを意味します、基本的にあなたのケースでよりpythonicになるものは次のようなものです:
def myfunc(val):
cmd_type = 'i'
# forget about passing type to your magicprogram
cmdline = 'magicprogram %s ' % val
Popen(cmdline, ... blah blah)
そしてあなたのマジックプログラムで(それがあなたのスクリプトなのかどうかはわかりません...)、そしてすべての場合にあなたのプログラムは文字列を取得するので、スクリプトが受け入れるものに変換してみてください。
from optparse import OptionParser
# ....
if __name__ == '__main__':
parser = OptionParser(usage="blah blah")
# ...
(options, args) = parser.parse_args()
# Here you apply the EAFP with all type accepted.
try:
# call the function that will deal with if arg is string
# remember duck typing.
except ... :
# You can continue here
すべてのコードが何であるかはわかりませんが、上記の例に従うと、よりPythonicになり、すべてのルールに例外があることを覚えているので、おそらくあなたのケースは例外であり、型チェックを行う方がよいでしょう.
これで問題が解決することを願っています。