How to efficiently extract coordinates from a `pybedtools.Interval`? I'm using pybedtools to process data, and at some point, I loop over a BedTool object and use unpacking to extract coordinates from the Interval:

for interval in my_bedtool:
    (chrom, start, end, _, _, strand) = interval


Suppose I want to compute the interval length, as follows:

    length = end - start


This results in a TypeError: unsupported operand type(s) for -: 'str' and 'str'.

It happens that the unpacked end and start variables are strings.
Converting them to ints is probably suboptimal given that Intervals can be created by providing ints for the coordinates:

from pybedtool import Interval
interval = Interval("X", 0, 100, strand="+")


The start and end attributes of an Interval are ints.

I could do:

    (chrom, start, end, strand) = (
        interval.chrom, interval.start, interval.end, interval.strand)


But I think this looks a bit ugly.

Is there a better approach?
The reason why unpacking an Interval results in strings instead of ints might be related to how indexing by position works.

Even without considering the string/int issue, it seems slightly more efficient to access the interval's attributes rather than unpacking them directly:

In [311]: %timeit [(chrom, start, end, strand) for (chrom, start, end, _, _, strand) in my_bedtool]
306 ms ± 26.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [312]: %timeit [(interval.chrom, interval.start, interval.end, interval.strand) for interval in my_bedtool]
238 ms ± 24.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)


To resolve the ugliness issue, attribute access can be made more compact through the use of operator.attrgetter:

In [313]: from operator import attrgetter

In [314]: get_interval_attrs = attrgetter("chrom", "start", "stop", "strand")

In [315]: %timeit [get_interval_attrs(interval) for interval in my_bedtool]
239 ms ± 26.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)