python2.7 で
以下の
発生原因と、'builtin_function_or_method'
と、'__getitem__'
に
typeerror 'builtin_function_or_method' object has no attribute '__getitem__'
原因
プログラムの
ids.append[blog_post.id]
[]
記述なので、対処方法
正しくは[]
ではなく、()
でした。
ids.append(blog_post.id)
そもそも builtin_function_or_method
とは 何か
builtin_function_or_method
なので、
buildin_function
または、method
に
2つに
builtin_function に ついて
python の
組み
* 2. 組み
* 2. 組み
また、
>>> import pprint >>> pprint.pprint(dir(__builtins__)) ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip'] >>>
method に ついて
関数とmethod
って
関数と
など
ざっくり関数
で、
class内にメソッド
と
関数はstaticな<wbr>メソッド
に
と
モジュールに ついて
モジュールって
6. モジュール (module) — Python 3.6.1 ドキュメント
の
改めて、 どんな 時に、typeerror 'builtin_function_or_method' object has no attribute '__getitem__'
が 発生するか
改めて、typeerror 'builtin_function_or_method' object has no attribute '__getitem__'
が
list に
存在しない メソッド呼び出し
list を生成し、 存在しない メソッド呼び出しを してみます。 >>> a = [] >>> a.a Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'list' object has no attribute 'b'
AttributeError: 'list' object has no attribute 'b'
となります。 生成した
リストaに a() で アクセス >>> a = [] >>> a() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'list' object is not callable
TypeError: 'list' object is not callable
となります。 生成した
リストa の 要素に a[“a”] に して アクセス >>> a = [] >>> a["a"] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: list indices must be integers, not str
TypeError: list indices must be integers, not str
となります。 生成した
リストa の extend メソッドを [] で 呼び出す。 これは、>>> a = [] >>> a.extend[] File "<stdin>", line 1 a.extend[] ^ SyntaxError: invalid syntax
SyntaxError: invalid syntax
となります。 生成した
リストa の extend メソッドを [1] で 呼び出す。 これは、>>> a = [] >>> a.extend[1] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
となります。
__getitem__
ってなんかのかという ところですが、
[]
記載をすると、 __getitem__
がpython的には 呼ばれるようです。
__getitem__の挙動に ついて メモ - 素数好きの 最高技術責任者の ブログ
特殊メソッド名 - Dive Into Python 3 日本語版
3. データモデル — Python 3.6.1 ドキュメント
object has no attribute '__getitem__'
は部分は 理解できてきたのですが、
'builtin_function_or_method'
がどんな 時に 出力されるのかまだ、 腑に 落ちないので、
'builtin_function_or_method'
でなさそうな、requests
モジュールに対して 同じようなことを してみます。 requests.get("https://www.monotalk.xyz/sitemap.xml").json["aaaaaa"]
を呼び出す。
>>> import requests >>> requests.get("https://www.monotalk.xyz/sitemap.xml").json["aaaaaa"] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'instancemethod' object has no attribute '__getitem__'
TypeError: 'instancemethod' object has no attribute '__getitem__'
に
requests.get("https://www.monotalk.xyz/sitemap.xml")["aaaaaa"]
を呼び出す。
>>> import requests >>> requests.get("https://www.monotalk.xyz/sitemap.xml")["aaaaaa"] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'Response' object has no attribute '__getitem__'
TypeError: 'Response' object has no attribute '__getitem__'
にrequests.get["aaaaaa"]
を呼び出す。 >>> import requests >>> requests.get["aaaaaa"] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'function' object has no attribute '__getitem__'
TypeError: 'function' object has no attribute '__getitem__'
になりました。
雰囲気的に、builtins に 含まれる もので、 '__getitem__'
を実装していない
オブジェクトにアクセスすると、
typeerror 'builtin_function_or_method' object has no attribute '__getitem__'
が
builtins 内で、 ‘getitem‘ メソッドを 保持する オブジェクト
hasattr
で、__getitem__
を
>>> for k, v in __builtins__.__dict__.items(): ... if hasattr(v, "__getitem__"): ... print( k + " supports __getitem__ .") ... else: ... print( k + " not supports __getitem__. ") ... bytearray supports __getitem__ . IndexError supports __getitem__ . all not supports __getitem__. help not supports __getitem__. vars not supports __getitem__. SyntaxError supports __getitem__ . unicode supports __getitem__ . UnicodeDecodeError supports __getitem__ . memoryview supports __getitem__ . isinstance not supports __getitem__. copyright not supports __getitem__. NameError supports __getitem__ . BytesWarning supports __getitem__ . dict supports __getitem__ . input not supports __getitem__. oct not supports __getitem__. bin not supports __getitem__. SystemExit supports __getitem__ . StandardError supports __getitem__ . format not supports __getitem__. repr not supports __getitem__. sorted not supports __getitem__. False not supports __getitem__. RuntimeWarning supports __getitem__ . list supports __getitem__ . iter not supports __getitem__. reload not supports __getitem__. Warning supports __getitem__ . __package__ not supports __getitem__. round not supports __getitem__. dir not supports __getitem__. cmp not supports __getitem__. set not supports __getitem__. bytes supports __getitem__ . reduce not supports __getitem__. intern not supports __getitem__. issubclass not supports __getitem__. Ellipsis not supports __getitem__. EOFError supports __getitem__ . locals not supports __getitem__. BufferError supports __getitem__ . slice not supports __getitem__. FloatingPointError supports __getitem__ . sum not supports __getitem__. getattr not supports __getitem__. abs not supports __getitem__. exit not supports __getitem__. print not supports __getitem__. True not supports __getitem__. FutureWarning supports __getitem__ . ImportWarning supports __getitem__ . None not supports __getitem__. hash not supports __getitem__. ReferenceError supports __getitem__ . len not supports __getitem__. credits not supports __getitem__. frozenset not supports __getitem__. __name__ supports __getitem__ . ord not supports __getitem__. super not supports __getitem__. _ supports __getitem__ . TypeError supports __getitem__ . license not supports __getitem__. KeyboardInterrupt supports __getitem__ . UserWarning supports __getitem__ . filter not supports __getitem__. range not supports __getitem__. staticmethod not supports __getitem__. SystemError supports __getitem__ . BaseException supports __getitem__ . pow not supports __getitem__. RuntimeError supports __getitem__ . float not supports __getitem__. MemoryError supports __getitem__ . StopIteration supports __getitem__ . globals not supports __getitem__. divmod not supports __getitem__. enumerate not supports __getitem__. apply not supports __getitem__. LookupError supports __getitem__ . open not supports __getitem__. quit not supports __getitem__. basestring not supports __getitem__. UnicodeError supports __getitem__ . zip not supports __getitem__. hex not supports __getitem__. long not supports __getitem__. next not supports __getitem__. ImportError supports __getitem__ . chr not supports __getitem__. xrange supports __getitem__ . type not supports __getitem__. __doc__ supports __getitem__ . Exception supports __getitem__ . tuple supports __getitem__ . UnicodeTranslateError supports __getitem__ . reversed not supports __getitem__. UnicodeEncodeError supports __getitem__ . IOError supports __getitem__ . hasattr not supports __getitem__. delattr not supports __getitem__. setattr not supports __getitem__. raw_input not supports __getitem__. SyntaxWarning supports __getitem__ . compile not supports __getitem__. ArithmeticError supports __getitem__ . str supports __getitem__ . property not supports __getitem__. GeneratorExit supports __getitem__ . int not supports __getitem__. __import__ not supports __getitem__. KeyError supports __getitem__ . coerce not supports __getitem__. PendingDeprecationWarning supports __getitem__ . file not supports __getitem__. EnvironmentError supports __getitem__ . unichr not supports __getitem__. id not supports __getitem__. OSError supports __getitem__ . DeprecationWarning supports __getitem__ . min not supports __getitem__. UnicodeWarning supports __getitem__ . execfile not supports __getitem__. any not supports __getitem__. complex not supports __getitem__. bool not supports __getitem__. ValueError supports __getitem__ . NotImplemented not supports __getitem__. map not supports __getitem__. buffer supports __getitem__ . max not supports __getitem__. object not supports __getitem__. TabError supports __getitem__ . callable not supports __getitem__. ZeroDivisionError supports __getitem__ . eval not supports __getitem__. __debug__ not supports __getitem__. IndentationError supports __getitem__ . AssertionError supports __getitem__ . classmethod not supports __getitem__. UnboundLocalError supports __getitem__ . NotImplementedError supports __getitem__ . AttributeError supports __getitem__ . OverflowError supports __getitem__ .
まとめ
まとめます。
typeerror 'builtin_function_or_method' object has no attribute '__getitem__
は、__getitem__
を[]
記載で
出力される
ほとんど(
と[
をtypo
して
まず、
以上です。
コメント