[Python-ideas] Respectively and its unpacking sentence

Mirmojtaba Gharibi mojtaba.gharibi at gmail.com
Wed Jan 27 12:55:33 EST 2016


On Wed, Jan 27, 2016 at 2:29 AM, Andrew Barnert <abarnert at yahoo.com> wrote:

> On Jan 26, 2016, at 22:19, Mirmojtaba Gharibi <mojtaba.gharibi at gmail.com>
> wrote:
>
> Yes, I'm aware sequence unpacking.
> There is an overlap like you mentioned, but there are things that can't be
> done with sequence unpacking, but can be done here.
>
> For example, let's say you're given two lists that are not necessarily
> numbers, so you can't use numpy, but you want to apply some component-wise
> operator between each component. This is something you can't do with
> sequence unpacking or with numpy.
>
>
> Yes, you can do it with numpy.
>
> Obviously you don't get the performance benefits when you aren't using
> "native" types (like int32) and operations that have vectorizes
> implementations (like adding two arrays of int32 or taking the dot product
> of float64 matrices), but you do still get the same elementwise operators,
> and even a way to apply arbitrary callables over arrays, or even other
> collections:
>
>     >>> firsts = ['John', 'Jane']
>     >>> lasts = ['Smith', 'Doe']
>     >>> np.vectorize('{1}, {0}'.format)(firsts, lasts)
>     array(['Smith, John', 'Doe, Jane'], dtype='<U11)
>
> I think the form I am suggesting is simpler and more readable. I'm happy
you brought vectorize to my attention though. I think as soon you make the
statement just a bit complex, it would become really complicated with
vectorize.

For example lets say you have
x=[1,2,3,4,5,...]
y=['A','BB','CCC',...]
p=[2,3,4,6,6,...]
r=[]*n

$r = str(len($y*$p)+$x)

It would be really complex to calculate such a thing with vectorize.
All I am saving on is basically a for-loop and the indexing. We don't
really have to use numpy,etc. I think it's much easier to just use for-loop
and indexing, if you don't like the syntax. So I think the question is,
does my syntax bring enough convenience to avoid for-loop and indexing.
For example the above could be equivalently written as
for i in range(0,len(r)):
...r[i] = str(len(y[i]*p[i])+x[i])
So that's the whole saving. Just a for-loop and indexing operator.




> That's everything you're asking for, with even more flexibility, with no
> need for any new ugly perlesque syntax: just use at least one np.array type
> in an operator expression, call a method on an array type, or wrap a
> function in vectorize, and everything is elementwise.
>
> And of course when you actually _are_ using numbers, as in every single
> one of your examples, using numpy also gives you around a 6:1 space and
> 20:1 time savings, which is a nice bonus.
>
> For example:
>
> $StudentFullName = $FirstName + " " + $LastName
>
> So, in effect, I think one big part of is component wise operations.
>
> Another thing that can't be achieved with sequence unpacking is:
> f($x)
> i.e. applying f for each component of x.
>
>
> That's a very different operation, which I think is more readably spelled
> map(f, x).
>
> About your question above, it's not ambiguous here either:
>  a; b = x1;a + x2;5
> is exactly "Equivalent" to
> a = x1+x2
> b = a + 5
>
> Also, there is a difference in style in sequence unpacking, and here.
> In sequence unpacking, you have to pair up the right variables and repeat
> the operator, for example:
> x,y,z = x1+x2 , y1+y2, z1+z2
> Here you don't have to repeat it and pair up the right variables, i.e.
> x;y;z = x1;y1;z1 + x2;y2;z2
>
>
> If you only have two or three of these, that isn't a problem. Although in
> this case, it sure looks like you're trying to add two 3D vectors, so
> maybe you should just be storing 3D vectors as instances of a class (with
> an __add__ method, of course), or as arrays, or as columns in a larger
> array, rather than as 3 separate variables. What could be more readable
> than this:
>
>     v = v1 + v2
>
> And if you have more than about three separate variables, you _definitely_
> want some kind of array or iterable, not a bunch of separate variables.
> You're worried about accidentally typing "y1-y2" when you meant "+", but
> you're far more likely to screw up one of the letters or numbers than the
> operator. You also can't loop over separate variables, which means you
> can't factor out some logic and apply it to all three axes, or to both
> vectors. Also consider how you'd do something like transposing or pivoting
> or anything even fancier. If you've got a 2D array or iterable of
> iterables, that's trivial: transpose or zip, etc. If you've got N*M
> separate variables, you have to write them all individually. Your syntax at
> best cuts the source length and opportunity for errors in half; using
> collections cuts it down to 1.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160127/a848dcb1/attachment.html>


More information about the Python-ideas mailing list