classification
Title: Unable to inherit bytes: bytes.__init__() doesn't accept arguments
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.0
process
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 2008-08-21 10:16 by pitrou. This issue is now closed.

Messages (2)
msg71619 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-08-21 09:54
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.
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'foo',)
>>> b
b'foo'
>>> type(b)
<class '__main__.B'>
History
Date User Action Args
2008-08-21 10:16:40pitrousetstatus: open -> closed
resolution: not a bug
messages: + msg71623
nosy: + pitrou
2008-08-21 09:54:15vstinnercreate