• RE: super().__init__() and bytes

    From Anders Munch@ajm@flonidan.dk to comp.lang.python on Tue Dec 3 12:55:59 2024
    From Newsgroup: comp.lang.python

    Roel Schroeven <roel@roelschroeven.net> wrote:
    As a follow-up, it looks like this behavior is because bytes and int are immutable.

    Yes.

    But that doesn't tell me why using super().__init__(<custom arguments>) doesn't work for immutable classes.

    bytes.__init__ does work, but it's just an inherited object.__init__, which does nothing, and takes no parameters.
    __init__ cannot change the value of the bytes object; the value is set by bytes.__new__ and cannot change after that.

    Best not to define an __init__ method at all, just use __new__.

    Something like:

    class BytesSubclass(bytes):
    def __new__(cls, whatever, arguments, you, like):
    bytesvalue = compute(whatever, arguments, you, like)
    ob = bytes.__new__(cls, bytesvalue)
    ob.some_other_att = compute_something_else(whatever, arguments, you, like)
    return ob

    regards,
    Anders

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Roel Schroeven@roel@roelschroeven.net to comp.lang.python on Tue Dec 3 15:24:55 2024
    From Newsgroup: comp.lang.python

    Op 3/12/2024 om 13:55 schreef Anders Munch via Python-list:
    Roel Schroeven <roel@roelschroeven.net> wrote:
    As a follow-up, it looks like this behavior is because bytes and int are immutable.

    Yes.
    OK.
    But that doesn't tell me why using super().__init__(<custom arguments>) doesn't work for immutable classes.

    bytes.__init__ does work, but it's just an inherited object.__init__, which does nothing, and takes no parameters.
    __init__ cannot change the value of the bytes object; the value is set by bytes.__new__ and cannot change after that.

    I see now why __init__, being a regular method, can't change an object's
    value (or attributes in general) if that object is immutable. I'm not
    sure why I didn't think of that before.

    It's not entirely clear to me though how bytes.__new__ *can* set an
    object's value. Isn't __new__ also a regular function? Are these
    immutable classes special cases in the language that can't be recreated
    in the same way with user-defined classes? Not that that's something I
    want to do, and it's also not terribly important to me, but I'm trying
    to better understand what's going on.
    Best not to define an __init__ method at all, just use __new__.

    Something like:

    class BytesSubclass(bytes):
    def __new__(cls, whatever, arguments, you, like):
    bytesvalue = compute(whatever, arguments, you, like)
    ob = bytes.__new__(cls, bytesvalue)
    ob.some_other_att = compute_something_else(whatever, arguments, you, like)
    return ob
    Thanks, that works perfectly. That's also more important than
    understanding all the nitty-gritty details (I feel a basic understanding
    is important, but not necessarily always all the low-level details).
    --
    "There is no cause so noble that it will not attract fuggheads."
    -- Larry Niven

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Greg Ewing@greg.ewing@canterbury.ac.nz to comp.lang.python on Wed Dec 4 12:14:17 2024
    From Newsgroup: comp.lang.python

    On 4/12/24 3:24 am, Roel Schroeven wrote:
    It's not entirely clear to me though how bytes.__new__ *can* set an
    object's value. Isn't __new__ also a regular function?

    Yes, but the __new__ methods of the builtin immutable objects (int,
    str, bytes, etc.) are implemented in C, and so are able to do things
    that Python methods cannot.
    --
    Greg

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Roel Schroeven@roel@roelschroeven.net to comp.lang.python on Wed Dec 4 12:38:33 2024
    From Newsgroup: comp.lang.python

    Op 4/12/2024 om 0:14 schreef Greg Ewing via Python-list:
    On 4/12/24 3:24 am, Roel Schroeven wrote:
    It's not entirely clear to me though how bytes.__new__ *can* set an
    object's value. Isn't __new__ also a regular function?

    Yes, but the __new__ methods of the builtin immutable objects (int,
    str, bytes, etc.) are implemented in C, and so are able to do things
    that Python methods cannot.
    Aha, yes, that's what I already suspected, but I wasn't sure. Thanks for confirming that.

    All clear now. Thanks to Anders and Greg for explaining this to me.

    "In the old days, writers used to sit in front of a typewriter and stare out of the window. Nowadays, because of the marvels of convergent technology, the thing
    you type on and the window you stare out of are now the same thing.”
    -- Douglas Adams

    --- Synchronet 3.20a-Linux NewsLink 1.114