On Wed, May 08, 2019 at 08:14:50AM +1000, Cameron Simpson wrote:
> On 08May2019 00:18, Greg Ewing <greg.ew...@canterbury.ac.nz> wrote:
> >Steven D'Aprano wrote:
> >>That suggests a possible pair of constructors:
> >>   bytes.from_int(n)  -> equivalent to b'%d' % n
> >>   bytes.ord(n)       -> equivalent to bytes((n,))
> >
> >I don't see how bytes.from_int(n) is any clearer about what it
> >does than just bytes(n). If we're going to have named constructors,
> >the names should indicate *how* the construction is being done,
> >not just repeat the type of the argument.
> 
> How about from_size(n) and from_ord(n)? The former to make a NUL filled 
> bytes of size n, the latter to make a single byte bytes with element 0 
> having value n.

We already have from_size, it's just spelled bytes(n).

I don't dislike from_ord as a name, although perhaps it ought to be 
fromord to match fromhex.


> Preemptively one could argue for from_size having an optional fill 
> value, default 0.  I think I'm -0 on that because I can't imagine a 
> likely use case and it would slightly slow down every use.

Wanting to fill a bytes object with something other than zeroes is 
probably uncommon. But for those who need it '\xFF'*n should do the job.


> I think I'd 
> argue for a from_iter instead, to support an arbitrary fill 
> pattern/sequence.

We already have that, its spelled bytes(iterable).

py> bytes(range(5))
b'\x00\x01\x02\x03\x04'



-- 
Steven
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to