次のステートメントで型エラーが発生する理由を理解できません
log.debug('vec : %s blasted : %s\n' %(str(vec), str(bitBlasted)))
type(vec) is unicode
bitBlasted is a list
次のエラーが表示されます
TypeError: 'str' object is not callable
コリンが言ったように、ビルトインをシャドーイングしている可能性がありますstr:
>>> str = some_variable_or_string #this is wrong
>>> str(123.0) #Or this will happen
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
1つの解決策は、変数名をstr_または何かに変更することです。より良い解決策は、この種のハンガリー語の命名システムを避けることです。これは Java ではありません。Python のポリモーフィズムを最大限に利用し、代わりによりわかりやすい名前を使用してください。
別の可能性として、オブジェクトに適切な__str__メソッドがないか、まったくない可能性があります。
Python がメソッドをチェックするstr方法は次のとおりです。
__str__クラスのメソッド__str__親クラスのメソッド__repr__クラスのメソッド__repr__親クラスのメソッド<module>.<classname> instance at <address>where <module>is self.__class__.__module__、<classname>is self.__class__.__name__、<address>isの形式の文字列id(self)__str__新しいメソッドを使用するよりもさらに優れ__unicode__ています (Python 3.x では、それらは__bytes__とです。その後、スタブ メソッドとして__str__実装できます。__str__
class foo:
...
def __str__(self):
return unicode(self).encode('utf-8')
詳細については、この質問を参照してください。
mouad が言ったようstrに、ファイルの上位の場所で名前を使用しました。それは既存のビルトインstrを隠し、エラーを引き起こします。例えば:
>>> mynum = 123
>>> print str(mynum)
123
>>> str = 'abc'
>>> print str(mynum)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable