This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Unable to inherit bytes: bytes.__init__() doesn't accept arguments
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.0
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: pitrou, vstinner
Priority: normal Keywords:

Created on 2008-08-21 09:54 by vstinner, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg71619 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-08-21 09:54

   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.
msg71623 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-08-21 10:16
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
>>> type(b)
<class '__main__.B'>
Date User Action Args
2022-04-11 14:56:37adminsetgithub: 47880
2008-08-21 10:16:40pitrousetstatus: open -> closed
resolution: not a bug
messages: + msg71623
nosy: + pitrou
2008-08-21 09:54:15vstinnercreate