Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Python 3 __bytes__ method

730 views
Skip to first unread message

Ethan Furman

unread,
Jan 11, 2014, 7:24:46 PM1/11/14
to Python
Python 3 has a new method, __bytes__. The docs say: Called by bytes() to compute a byte-string representation of an
object. This should return a bytes object.

I must admit I'm not entirely clear how this should be used. Is anyone using this now? If so, how?

--
~Ethan~

Ethan Furman

unread,
Jan 11, 2014, 7:56:16 PM1/11/14
to Python
On 01/11/2014 04:53 PM, Daniel da Silva wrote:
>
> Where did you read this? I can't find any documentation about __bytes__ on google.

http://docs.python.org/3.3/reference/datamodel.html?highlight=__bytes__#object.__bytes__

--
~Ethan~

Daniel da Silva

unread,
Jan 11, 2014, 7:53:06 PM1/11/14
to Ethan Furman, Python
Where did you read this? I can't find any documentation about __bytes__ on google.

Regards,
Daniel


Ethan Furman

unread,
Jan 11, 2014, 9:44:55 PM1/11/14
to Python
On 01/11/2014 06:19 PM, Daniel da Silva wrote:
>
> One use case is:
> Suppose you have existing function that accepts a /bytes/ object. If you subclass /bytes/ and want it to be guaranteed
> to work with that function, you can override/__bytes__()/ to use the logistics of your subclass implementation.

I don't think so, for two reasons:

1) bytes objects do not have a __bytes__ method,

2) if the function is expecting a bytes object, it is unlikely to call bytes() on it.

--
~Ethan~

Daniel da Silva

unread,
Jan 11, 2014, 9:19:01 PM1/11/14
to Ethan Furman, Python
One use case is:
Suppose you have existing function that accepts a bytes object. If you subclass bytes and want it to be guaranteed to work with that function, you can override __bytes__() to use the logistics of your subclass implementation.


On Sat, Jan 11, 2014 at 7:56 PM, Ethan Furman <et...@stoneleaf.us> wrote:
On 01/11/2014 04:53 PM, Daniel da Silva wrote:

Where did you read this? I can't find any documentation about __bytes__ on google.

Ethan Furman

unread,
Jan 12, 2014, 12:31:41 AM1/12/14
to Python
On 01/11/2014 08:56 PM, Daniel da Silva wrote:
>
> I agree with you that realistic use cases are hard to think of.
>
> Does that answer your question better?

Well, since I was asking if anybody was already using the feature, no. ;)

--
~Ethan~

Daniel da Silva

unread,
Jan 11, 2014, 11:56:42 PM1/11/14
to Ethan Furman, Python



On Sat, Jan 11, 2014 at 9:44 PM, Ethan Furman <et...@stoneleaf.us> wrote:
On 01/11/2014 06:19 PM, Daniel da Silva wrote:

One use case is:
Suppose you have existing function that accepts a /bytes/ object. If you subclass /bytes/ and want it to be guaranteed
to work with that function, you can override/__bytes__()/ to use the logistics of your subclass implementation.

I don't think so, for two reasons:

1) bytes objects do not have a __bytes__ method,

2) if the function is expecting a bytes object, it is unlikely to call bytes() on it.

In general __typename__() methods are for explicit typename(obj) conversion. There is __int__(), __str__(), etc. They are what is behind int('3') == 3 and str(4) == '4'. If for no other reason, __bytes__() is there for symmetry. I agree with you that realistic use cases are hard to think of.

Does that answer your question better? 

All the best,
Daniel

Thomas Rachel

unread,
Jan 15, 2014, 7:07:46 AM1/15/14
to
Am 12.01.2014 01:24 schrieb Ethan Furman:

> I must admit I'm not entirely clear how this should be used. Is anyone
> using this now? If so, how?

I am not, as I currently am using Py2, but if I would, I would do it e.
g. for serialization of objects in order to send them over the line or
to save them into a file. IOW, the same purpose as we havd on __str__ in
Py2.


Thomas

0 new messages