__index__

duncan smith buzzard at invalid.invalid
Sat Nov 1 15:41:33 EDT 2014


On 01/11/14 18:29, Ethan Furman wrote:
> On 11/01/2014 10:11 AM, Ned Batchelder wrote:
>> On 11/1/14 12:56 PM, duncan smith wrote:
>>>
>>>        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__, it doesn't seem to work as I expected it to.
>>
>> 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__
> 
> In Python 2 __oct__ and __hex__ are used for oct() and hex(), but in
> Python 3 __index__ is used.
> 

It was the doc for hex at
https://docs.python.org/2/library/functions.html that led me to think I
needed to implement the _index__ method. The doc for bin and oct seems
to be right (I need __index__ for bin, but it doesn't work for oct).


> But I agree with Net that using a separate method is probably better.
> 
> -- 
> ~Ethan~

Possibly, I'm still tinkering with it. I use the Bloom filters to
generate, and act as pseudonyms for token sets. I have another class
that represents pseudonyms (of a different kind, but still bit strings)
that is derived from long - so hex etc. just work. I should probably (at
least) subclass my Bloom filter class before adding the relevant
methods. Cheers.

Duncan



More information about the Python-list mailing list