Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80096
| Date | 2014-11-01 16:56 +0000 |
|---|---|
| From | duncan smith <buzzard@invalid.invalid> |
| Newsgroups | comp.lang.python |
| Subject | __index__ |
| Message-ID | <545510c7$0$27750$862e30e2@ngroups.net> (permalink) |
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
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
__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
csiph-web