Skip to content

bytes does not implement __bytes__() #77236

Closed

Description

@FHTMitchell
BPO 33055
Nosy @terryjreedy, @serhiy-storchaka, @FHTMitchell
Superseder
  • bpo-24234: Should we define complex.complex and bytes.bytes?
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-03-29.00:04:41.868>
    created_at = <Date 2018-03-12.14:28:02.041>
    labels = ['interpreter-core', '3.8', 'type-bug', '3.7']
    title = 'bytes does not implement __bytes__()'
    updated_at = <Date 2018-03-29.00:04:41.867>
    user = 'https://github.com/FHTMitchell'

    bugs.python.org fields:

    activity = <Date 2018-03-29.00:04:41.867>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-03-29.00:04:41.868>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2018-03-12.14:28:02.041>
    creator = 'FHTMitchell'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33055
    keywords = []
    message_count = 4.0
    messages = ['313653', '313666', '313977', '314624']
    nosy_count = 3.0
    nosy_names = ['terry.reedy', 'serhiy.storchaka', 'FHTMitchell']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '24234'
    type = 'behavior'
    url = 'https://bugs.python.org/issue33055'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8']

    Activity

    1. FHTMitchell commented on Mar 12, 2018

      FHTMitchellmannequin
      MannequinAuthor

      Every object which has a corresponding dunder protocol also implements said protocol with one exception:

      >>> 'hello'.__str__()
      'hello'
      
      >>> (3.14).__float__()
      3.14
      
      >>> (101).__int__()
      101
      
      >>> True.__bool__()
      True
      
      >>> iter(range(10)).__iter__()
      <range_iterator at 0xf6b08b0>

      >> b'hello'.__bytes__()
      ---------------------------------------------------------------------------

      AttributeError                            Traceback (most recent call last)
      ----> 1 b'hello'.__bytes__()

      AttributeError: 'bytes' object has no attribute '__bytes__'

      This was brought up on SO as being inconsistent: https://stackoverflow.com/questions/49236655/bytes-doesnt-have-bytes-method/49237034?noredirect=1#comment85477673_49237034

    2. serhiy-storchaka commented on Mar 12, 2018

      @serhiy-storchaka
      Member

      This isn't one exception. For example complex doesn't have the __complex__ method.

      __str__, __float__, __int__ and __bool__ are implemented in str, float, int and bool because there are corresponding special slots in a type object. But __bytes__ and __complex__ were added later and they have no slots. Calling them for bytes and complex will slowdown creating new object.

      The user code rarely needs to call these methods directly. Just call bytes(), complex(), str(), float(), etc.

    3. terryjreedy commented on Mar 16, 2018

      @terryjreedy
      Member

      It is against Guido's general policy to add things not needed purely for consistency. Unless there is a compelling use case, I think this should be closed.

      FHTM: I don't think Serhiy posts on SO, so you might consider adding his answer to yours, or make it a separate answer.

    4. serhiy-storchaka commented on Mar 29, 2018

      @serhiy-storchaka
      Member

      In any case this is a duplicate of 24234

    5. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)end of life3.8 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions