[Python-ideas] More alternate constructors for builtin type
Cameron Simpson
cs at cskk.id.au
Tue May 7 18:14:50 EDT 2019
On 08May2019 00:18, Greg Ewing <greg.ewing at 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.
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. I think I'd
argue for a from_iter instead, to support an arbitrary fill
pattern/sequence. That avoids slowing the common case and provides
flexibility at the same time.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Python-ideas
mailing list