为什么tf.Variable是可迭代的但不能迭代

Pek*_*kka 2 python iterable python-2.7 tensorflow

为什么这是真的:

from collections import Iterable
import tensorflow as tf

v = tf.Variable(1.0)
print(isinstance(v, Iterable))

True
Run Code Online (Sandbox Code Playgroud)

而这个

iter(v)
Run Code Online (Sandbox Code Playgroud)

TypeError: 'Variable' object is not iterable.
Run Code Online (Sandbox Code Playgroud)

Ali*_*lov 5

我在Tensorflow的Variable类中找到了以下方法:

def __iter__(self):
    """Dummy method to prevent iteration. Do not call.
    NOTE(mrry): If we register __getitem__ as an overloaded operator,
    Python will valiantly attempt to iterate over the variable's Tensor from 0
    to infinity.  Declaring this method prevents this unintended behavior.
    Raises:
      TypeError: when invoked.
    """
    raise TypeError("'Variable' object is not iterable.")
Run Code Online (Sandbox Code Playgroud)

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/variables.py#L366