Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80096 > unrolled thread

__index__

Started byduncan smith <buzzard@invalid.invalid>
First post2014-11-01 16:56 +0000
Last post2014-11-01 13:00 -0700
Articles 6 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  __index__ duncan smith <buzzard@invalid.invalid> - 2014-11-01 16:56 +0000
    Re: __index__ Ned Batchelder <ned@nedbatchelder.com> - 2014-11-01 13:11 -0400
    Re: __index__ duncan smith <buzzard@invalid.invalid> - 2014-11-01 17:15 +0000
    Re: __index__ Ethan Furman <ethan@stoneleaf.us> - 2014-11-01 11:29 -0700
      Re: __index__ duncan smith <buzzard@invalid.invalid> - 2014-11-01 19:41 +0000
    Re: __index__ Ethan Furman <ethan@stoneleaf.us> - 2014-11-01 13:00 -0700

#80096 — __index__

Fromduncan smith <buzzard@invalid.invalid>
Date2014-11-01 16:56 +0000
Subject__index__
Message-ID<545510c7$0$27750$862e30e2@ngroups.net>
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 "<pyshell#76>", line 1, in <module>
    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.

Duncan

[toc] | [next] | [standalone]


#80097

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-11-01 13:11 -0400
Message-ID<mailman.15393.1414861894.18130.python-list@python.org>
In reply to#80096
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 "<pyshell#76>", line 1, in <module>
>      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

[toc] | [prev] | [next] | [standalone]


#80098

Fromduncan smith <buzzard@invalid.invalid>
Date2014-11-01 17:15 +0000
Message-ID<54551548$0$27691$862e30e2@ngroups.net>
In reply to#80096
On 01/11/14 16:56, duncan smith wrote:

[snip]

Sorry, forgot to add that I'm using Python 2.7.6 on Ubuntu 14.04. Cheers.

Duncan

[toc] | [prev] | [next] | [standalone]


#80099

FromEthan Furman <ethan@stoneleaf.us>
Date2014-11-01 11:29 -0700
Message-ID<mailman.15394.1414866549.18130.python-list@python.org>
In reply to#80096
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.

But I agree with Net that using a separate method is probably better.

--
~Ethan~

[toc] | [prev] | [next] | [standalone]


#80101

Fromduncan smith <buzzard@invalid.invalid>
Date2014-11-01 19:41 +0000
Message-ID<5455376e$0$27681$862e30e2@ngroups.net>
In reply to#80099
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

[toc] | [prev] | [next] | [standalone]


#80102

FromEthan Furman <ethan@stoneleaf.us>
Date2014-11-01 13:00 -0700
Message-ID<mailman.15396.1414872024.18130.python-list@python.org>
In reply to#80096
On 11/01/2014 11:29 AM, Ethan Furman wrote:
>
> But I agree with Net ...

Oops, should have ben 'Ned' -- apologies!

--
~Ethan~

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web