Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!217.188.199.168.MISMATCH!takemy.news.telefonica.de!telefonica.de!newsfeed.xs4all.nl!newsfeed2.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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'argument': 0.05; 'converted': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'hex': 0.16; 'md5': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'this?': 0.23; 'header:User- Agent:1': 0.23; 'received:comcast.net': 0.24; 'class.': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'said,': 0.30; '"",': 0.31; '>>>>': 0.31; 'long.': 0.31; 'file': 0.32; 'class': 0.32; 'url:python': 0.33; '(most': 0.33; 'skip:_ 10': 0.34; "i'd": 0.34; "can't": 0.35; 'but': 0.35; 'add': 0.35; 'instances': 0.36; 'method': 0.36; 'useful': 0.36; 'url:org': 0.36; 'example,': 0.37; 'wrong': 0.37; 'expected': 0.38; 'implement': 0.38; 'filter': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'expect': 0.39; 'explain': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'simply': 0.61; 'making': 0.63; 'smith': 0.68; 'url:datamodel': 0.84; 'url:html#object': 0.84; 'url:reference': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: __index__ Date: Sat, 01 Nov 2014 13:11:25 -0400 References: <545510c7$0$27750$862e30e2@ngroups.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: c-50-133-228-126.hsd1.ma.comcast.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <545510c7$0$27750$862e30e2@ngroups.net> 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: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1414861894 news.xs4all.nl 2915 [2001:888:2000:d::a6]:54693 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80097 On 11/1/14 12:56 PM, duncan smith wrote: > Hello, > I have a Bloom filter class and want to (partially) serialize > instances using hex() or oct(). Instances are mutable, so I can't > inherit from long. I thought I'd found the answer when I came across > __index__, > https://docs.python.org/2/reference/datamodel.html#object.__index__. But > it doesn't seem to work as I expected it to. > > >>>> class MyClass(object): > def __init__(self): > self.val = 7 > def __index__(self): > return self.val > > >>>> x = MyClass() >>>> oct(x) > > Traceback (most recent call last): > File "", line 1, in > oct(x) > TypeError: oct() argument can't be converted to oct >>>> oct(x.__index__()) > '07' >>>> > > > Can someone please explain why my thinking is wrong on this? TIA. Just above your link in the docs is __oct__ and __hex__, which are used to implement oct() and hex(): https://docs.python.org/2/reference/datamodel.html#object.__oct__ That said, I would simply add a .hex() method on the class. I would never expect a complex thing like a Bloom filter to be turned into useful hex with the hex() builtin. For example, when making an MD5 hash, you use the md5.hexdigest() method, not hex(md5). -- Ned Batchelder, http://nedbatchelder.com