numpy - Convert custom class to standard Python type -
i working numpy
array called predictions
. playing around following code:
print type(predictions) print list(predictions)
the output was:
<type 'numpy.ndarray'>` [u'yes', u'no', u'yes', u'yes', u'yes']
i wondering how numpy
managed build ndarray
class converted list not own list
function, standard python function.
python version: 2.7, numpy version: 1.9.2
i have answered pure python perspective below,
numpy
's arrays implemented in c - see e.g. thearray_iter
function.
the documentation defines argument list
iterable
; new_list = list(something)
works little bit like:
new_list = [] element in something: new_list.append(element)
(or, in list comprehension: new_list = [element element in something]
). therefore implement behaviour custom class, need define __iter__
magic method:
>>> class demo(object): def __iter__(self): return iter((1, 2, 3)) >>> list(demo()) [1, 2, 3]
Comments
Post a Comment