[SciPy-user] once again about zeros() and ones()

Matthieu Brucher matthieu.brucher at gmail.com
Sun Jul 1 08:45:12 EDT 2007


2007/7/1, dmitrey <openopt at ukr.net>:
>
> Hi all,
> There already was some discussion about zeros() and ones() usage, but I
> still can't understand some things.
> 1) currently ones(4,5, **kwargs) just yields error. Why it can't be
> translated to ones((4,5), **kwargs) ?
> See my example of Ones() below.
> ##############################
> from numpy import ones
> def Ones(*args, **kwargs):
>     if type(args[0]) in (type(()), type([])):
>         return ones(*args, **kwargs)
>     else: return ones(tuple(args), **kwargs)
> #############################
> if __name__ == '__main__':
>     print Ones((2,2))
>     print 2*Ones([2,2])
>     print 3*Ones(2,2)
>     print 4*Ones(2,2,2, dtype=float)
>     print 5*Ones((2,2,2), dtype='float')
>     print 6*Ones([2,2,2], dtype='int')
>     print 7*Ones(3,3, dtype=int, order = 'C')
>     print 8*Ones((3,3), dtype='int')
>     print 9*Ones(3, dtype='int')
>     print 10*Ones((3,), dtype='int')
> #############################
>

The first argument is a shape, the second is the type of the data. Python
has no way of knowing that the second argument is part of the shape and not
the type. This way, the arguments are defined correctly, although you could
make a wrapper *args, **kwargs and forcing people to use explicitely
dtype=float, which they won't do, thus leading to a lot of trouble and
errors.

Matthieu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20070701/8e61657a/attachment.html>


More information about the SciPy-User mailing list