<div dir="ltr">I know of many use cases for frozenset({...}) and I think a hack along those lines is fine -- but what's the common use for bytearray(b"...") or bytearray((...))? Almost invariably a bytearray is created empty or as a given number of zeros. I certainly wouldn't want to burden the tuple type with a to_bytearray() method, the types are unrelated (unlike set and frozenset).<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jul 11, 2018 at 6:03 PM, Robert Vanden Eynde <span dir="ltr"><<a href="mailto:robertvandeneynde@hotmail.com" target="_blank">robertvandeneynde@hotmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>
<div dir="ltr">
<div>
<div>
<div>{1,2,7}.freeze() or {1,2,7}.to_frozenset() seem very natural and if this can be optimized to avoid the copy, it's perfect.<br>
</div>
For bytearray, one use case would be to optimise bytearray([1,2,7,2]) in something like [1,2,7,2].to_byterray().<br>
About bytes, one could have (1,2,7,2).to_bytes() instead of bytes((1,2,7,2)) because b'\x01\x02\x07\x02' is long and boring.<br>
</div>
What about variables in the values {1,2,x}.freeze() should work too ?<br>
</div>
bytes((1,2,7,x)) is not writable as a b string and creates a copy.<br>
<div>
<div><br>
</div>
</div>
</div><div><div class="h5">
<br>
<div class="gmail_quote">
<div dir="ltr">Le jeu. 12 juil. 2018 à 02:24, Chris Angelico <<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>> a écrit :<br>
</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Thu, Jul 12, 2018 at 10:13 AM, Gregory P. Smith <<a href="mailto:greg@krypto.org" target="_blank">greg@krypto.org</a>> wrote:<br>
><br>
> On Wed, Jul 11, 2018 at 4:45 PM Jelle Zijlstra <<a href="mailto:jelle.zijlstra@gmail.com" target="_blank">jelle.zijlstra@gmail.com</a>><br>
> wrote:<br>
>> This could be done safely and without too much craziness if .freeze() on a<br>
>> set returned a new frozenset. The compiler could then safely optimize {a,<br>
>> set, literal}.freeze() into a frozenset literal, because methods on builtin<br>
>> types cannot be monkeypatched. There's been talk of a similar optimization<br>
>> on calls to .format() on string literals (not sure whether it's been<br>
>> implemented).<br>
>><br>
>> Whether implementing that is a good use of anyone's time is a different<br>
>> question.<br>
><br>
><br>
> Neat optimization. I hadn't considered that. We do know for sure it is a<br>
> builtin type at that point.<br>
><br>
> If that were implemented, bytes objects could gain a to_bytearray() (along<br>
> the lines of the int.to_bytes() API) method that could be optimized away in<br>
> literal circumstances.<br>
<br>
Be careful: a bytearray is mutable, so this isn't open to very many<br>
optimizations. A .freeze() method on sets would allow a set display to<br>
become a frozenset "literal", stored as a constant on the<br>
corresponding function object, the way a tuple is; but that's safe<br>
because the frozenset doesn't need to concern itself with identity,<br>
only value. Example:<br>
<br>
def f(x):<br>
a = (1, 2, 3) # can be optimized<br>
b = (x, 4, 5) # cannot<br>
c = [6, 7, 8] # cannot<br>
<br>
Disassemble this or look at f.__code__.co_consts and you'll see (1, 2,<br>
3) as a single constant; but the others have to be built.<br>
<br>
+1 on set.freeze(); +0 on bytes.to_bytearray().<br>
<br>
ChrisA<br>
______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">
http://python.org/psf/<wbr>codeofconduct/</a><br>
</blockquote>
</div>
</div></div></div>
<br>______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div>