Syntax not understood
Chris Angelico
rosuav at gmail.com
Thu Nov 4 15:28:34 EDT 2021
On Fri, Nov 5, 2021 at 6:23 AM <2QdxY4RzWzUUiLuE at potatochowder.com> wrote:
>
> On 2021-11-04 at 14:36:48 -0400,
> David Lowry-Duda <david at lowryduda.com> wrote:
>
> > > x_increment, y_increment = (scale * i for i in srcpages.xobj_box[2:])
> > >
> > > (scale * i for i in srcpages.xobj_box[2:]) is a generator, a single
> > > object, it should not be possible to unpack it into 2 variables.
> >
> > If you know the exact number of values in the generator, you can do
> > this. Here is an oversimplified example.
> >
> > l = [0, 1, 2, 3, 4, 5]
> > a, b = (elem * 10 for elem in l[:4])
> > print(a, b) # prints 40 50
> >
> > This is very fragile code and I would recommend against using it.
>
> How is that any more fragile than any other operation that destructs (or
> doesn't) a tuple?
>
The only part that's fragile, in my opinion, is (from the initial
post) that it's using removal syntax to slice off some, which is
fragile against the original input length:
x_increment, y_increment = (scale * i for i in srcpages.xobj_box[2:])
But since it says "box", I would start by assuming that it has four
elements. (They might be x1,y1,x2,y2 or x,y,w,h but it'll almost
always be four.) So it's not TOO fragile, when working with boxes, but
it is a little less clear that thing[2:] will always give exactly two
results.
ChrisA
More information about the Python-list
mailing list