Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.026 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'failing': 0.07; 'constructor': 0.09; 'from:charset:iso-8859-2': 0.09; 'lawrence': 0.09; 'read-only': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'bytearray': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'trying': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'large,': 0.24; 'regardless': 0.24; 'equivalent': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'especially': 0.30; 'url:python': 0.33; 'subject:from': 0.34; "can't": 0.35; 'opposed': 0.36; 'url:org': 0.36; 'should': 0.36; 'initially': 0.38; 'url:library': 0.38; 'to:addr:python- list': 0.38; 'expensive': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'url:3': 0.61; 'simply': 0.61; 'first': 0.61; 'name': 0.63; 'charset:iso-8859-2': 0.64; 'occur': 0.65; 'received:93': 0.72; 'subject:get': 0.81; 'flag.': 0.84; 'received:hr': 0.84; 'good,': 0.91; 'many,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: =?ISO-8859-2?Q?Juraj_Ivan=E8i=E6?= Subject: Re: how to get bytes from bytearray without copying Date: Mon, 03 Mar 2014 09:52:25 +0100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-2; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 93-136-72-166.adsl.net.t-com.hr User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393836762 news.xs4all.nl 2929 [2001:888:2000:d::a6]:35150 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67518 On 3.3.2014. 1:49, Mark Lawrence wrote: > If your data is readonly why can't you simply read it as bytes in the > first place? Failing that from > http://docs.python.org/3/library/stdtypes.html#memoryview > > tobytes() - Return the data in the buffer as a bytestring. This is > equivalent to calling the bytes constructor on the memoryview. > > >>> m = memoryview(b"abc") > >>> m.tobytes() > b'abc' > >>> bytes(m) > b'abc' Initially it has to be a bytearray because I read this data from a socket. My point is that once I have a bytearray x, then m = memoryview(bytes(x)) is a very expensive way to make a read-only memoryview, opposed to m = memoryview(x) or (fictional) m = memoryview(x, force_readonly=True) especially if the x-es are many, large, and occur often. I feel like memoryview's __hash__ is trying to be to smart for its own good, and that it should just return the damn hash like its name suggests, regardless of the value of 'writable' flag.