Skip to content

Unable to inherit bytes: bytes.__init__() doesn't accept arguments #47880

Closed

Description

@vstinner
BPO 3630
Nosy @pitrou, @vstinner

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 2008-08-21.10:16:40.310>
created_at = <Date 2008-08-21.09:54:15.605>
labels = ['invalid', 'type-bug', 'library']
title = "Unable to inherit bytes: bytes.__init__() doesn't accept arguments"
updated_at = <Date 2008-08-21.10:16:40.303>
user = 'https://github.com/vstinner'

bugs.python.org fields:

activity = <Date 2008-08-21.10:16:40.303>
actor = 'pitrou'
assignee = 'none'
closed = True
closed_date = <Date 2008-08-21.10:16:40.310>
closer = 'pitrou'
components = ['Library (Lib)']
creation = <Date 2008-08-21.09:54:15.605>
creator = 'vstinner'
dependencies = []
files = []
hgrepos = []
issue_num = 3630
keywords = []
message_count = 2.0
messages = ['71619', '71623']
nosy_count = 2.0
nosy_names = ['pitrou', 'vstinner']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue3630'
versions = ['Python 3.0']

Activity

  1. vstinner commented on Aug 21, 2008

    @vstinner
    MemberAuthor

    Example:

       class MyBytes(bytes):
          def __init__(self, *args, **kw):
             bytes.__init__(self, *args, **kw)
       a = bytes(b"hello")   # ok
       b = MyBytes(b"hello") # error

    => DeprecationWarning: object.__init__() takes no parameters

    The example works fine in Python 2.6 but fails in Python 3.0.

  2. added
    stdlib
    type-bug
    on Aug 21, 2008
  3. pitrou commented on Aug 21, 2008

    @pitrou
    Member

    With immutable types, you must use __new__ instead. Passing the
    arguments to __init__ is too late since the object is immutable and it
    has already been built in memory, thus you can't initialize it with
    another value.

    >>> class B(bytes):
    ...  def __new__(cls, *args, **kargs):
    ...   print(args)
    ...   return bytes.__new__(cls, *args, **kargs)
    ...
    >>> b = B(b"foo")
    (b'foo',)
    >>> b
    b'foo'
    >>> type(b)
    <class '__main__.B'>
  4. 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

    stdlibtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions