Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bytes does not implement __bytes__() #77236

Closed
FHTMitchell mannequin opened this issue Mar 12, 2018 · 4 comments
Closed

bytes does not implement __bytes__() #77236

FHTMitchell mannequin opened this issue Mar 12, 2018 · 4 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@FHTMitchell
Copy link
Mannequin

FHTMitchell mannequin commented Mar 12, 2018

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']

    @FHTMitchell
    Copy link
    Mannequin Author

    FHTMitchell mannequin commented Mar 12, 2018

    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

    @FHTMitchell FHTMitchell mannequin added 3.7 (EOL) end of life 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Mar 12, 2018
    @serhiy-storchaka
    Copy link
    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.

    @terryjreedy
    Copy link
    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.

    @serhiy-storchaka
    Copy link
    Member

    In any case this is a duplicate of 24234

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants