[Numpy-discussion] Constructor parameters - was Re: dtype
Colin J. Williams
cjw at sympatico.ca
Wed Feb 22 13:19:05 CST 2006
Robert Kern wrote:
>Colin J. Williams wrote:
>>I've been trying to gain some understanding of dtype from the builtin
>>documentation and would appreciate advice.
>>I don't find anything in http://projects.scipy.org/scipy/numpy or
>>Chapter 2.1 of the book has a good overview, but little reference material.
>>In the following, dt= numpy.dtype
>>Some specific problems are flagged ** below.
>>| | ----------------------------------------------------------------------
>>| Data and other attributes defined here:
>>| | __new__ = <built-in method __new__ of type object> |
>>T.__new__(S, ...) -> a new object with type S, a subtype of
>>T ** What are the parameters? In other words,
>>what does ... stand for? **
>"""Recall that you create class instances by calling the class. When the class
>is a new-style class, the following happens when it is called. First, the
>class's __new__ method is called, passing the class itself as first argument,
>followed by any (positional as well as keyword) arguments received by the
>original call. This returns a new instance. Then that instance's __init__ method
>is called to further initialize it. (This is all controlled by the __call__
>method of the metaclass, by the way.)
>>** There is no __module__ attribute. How does one identify the modules
>>holding the code? **
>It's an extension type PyArray_Descr* in numpy/core/src/arrayobject.c .
Many thank for this. You have described the standard Python approach to
constructing an instance. As I understand it,
numpy uses the __new__ method, but not __init__, in most cases.
My interest is in " any (positional as well as keyword) arguments".
What should the user feed the constuctor? This isn't clear from the
From a Python user's point of view, the module holding the dtype class
appears to be multiarray.
The standard Python approach is to put the information in a __module__
attribute so that one doesn't have to go hunting around. Please see below.
While on the subject of the Standand Python aproach, class names usually
start with an upper case letter and the builtins have their own style,
ListType etc. numpy equates ArrayType to ndarray but ArrayType is
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy.core.multiarray as mu
['_ARRAY_API', '__doc__', '__file__', '__name__', '__version__',
', '_get_ndarray_c_version', 'arange', 'array', 'bigndarray',
'broadcast', 'can_cast', 'concatenate'
, 'correlate', 'dot', 'dtype', 'empty', 'error', 'flatiter',
'frombuffer', 'fromfile', 'fromstring',
'getbuffer', 'inner', 'lexsort', 'ndarray', 'newbuffer',
'register_dtype', 'scalar', 'set_numeric_o
ps', 'set_string_function', 'set_typeDict', 'typeinfo', 'where', 'zeros']
More information about the Numpy-discussion