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. the array_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] 

note conversion other types require different methods.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -