Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.030 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'binary': 0.07; 'method.': 0.07; 'arguments': 0.09; 'clean.': 0.09; 'method:': 0.09; 'former,': 0.16; 'hex': 0.16; 'mailscanner,': 0.16; 'str,': 0.16; 'typeerror:': 0.16; '>>>': 0.22; 'import': 0.22; 'header:User- Agent:1': 0.23; 'bytes': 0.24; 'instead.': 0.24; 'questions:': 0.24; 'subject:/': 0.26; 'function': 0.29; 'scanned': 0.29; '"",': 0.31; 'subject:skip:i 10': 0.31; 'file': 0.32; 'class': 0.32; '(most': 0.33; 'subject:from': 0.34; 'case,': 0.35; 'convert': 0.35; 'but': 0.35; 'there': 0.35; 'believed': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'dangerous': 0.60; 'viruses': 0.61; 'reverse': 0.68; 'obvious': 0.74; '"one': 0.84; 'it"': 0.84; 'same,': 0.91; 'str.': 0.91 Date: Sat, 16 Nov 2013 23:16:58 +0100 From: Laszlo Nagy User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 MIME-Version: 1.0 To: python-list@python.org Subject: inconsistency in converting from/to hex Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-shopzeus-MailScanner-Information: Please contact the ISP for more information X-shopzeus-MailScanner-ID: 125488895C52.AABD1 X-shopzeus-MailScanner: Found to be clean X-shopzeus-MailScanner-From: gandalf@shopzeus.com X-Spam-Status: No 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: 1384640221 news.xs4all.nl 16000 [2001:888:2000:d::a6]:39518 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59663 We can convert from hex str to bytes with bytes.fromhex class method: >>> b = bytes.fromhex("ff") But we cannot convert from hex binary: >>> b = bytes.fromhex(b"ff") Traceback (most recent call last): File "", line 1, in TypeError: must be str, not bytes We don't have bytes_instance.tohex() instance method. But we have binascii.hexlify. But binascii.hexlify does not return an str. It returns a bytes instance instead. >>> import binascii >>> binascii.hexlify(b) b'ff' Its reverse function binascii.unhexlify can be used on str and bytes too: >>> binascii.unhexlify(b'ff') b'\xff' >>> binascii.unhexlify('ff') b'\xff' Questions: * if we have bytes.fromhex() then why don't we have bytes_instance.tohex() ? * if the purpose of binascii.unhexlify and bytes.fromhex is the same, then why allow binary arguments for the former, and not for the later? * in this case, should there be "one obvious way to do it" or not? -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.