[issue3630] Unable to inherit bytes: bytes.__init__() doesn't accept arguments

Antoine Pitrou report at bugs.python.org
Thu Aug 21 12:16:42 CEST 2008


Antoine Pitrou <pitrou at free.fr> added the comment:

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

----------
nosy: +pitrou
resolution:  -> invalid
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3630>
_______________________________________


More information about the Python-bugs-list mailing list