Syntax not understood

2QdxY4RzWzUUiLuE at potatochowder.com 2QdxY4RzWzUUiLuE at potatochowder.com
Thu Nov 4 15:44:20 EDT 2021


On 2021-11-05 at 06:28:34 +1100,
Chris Angelico <rosuav at gmail.com> wrote:

> 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.

I won't disagree that most boxes are going to be described by four
elements, in which case slicing off the first two *should* leave two
more (presumably x_increment and y_increment).

So if you're saying that all that's missing is a description of
srcpages.xobj_box, then I agree.  :-)  But such is life on a mailing
list where posters don't always give their questions the context they
deserve.


More information about the Python-list mailing list