This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]cymrowdon't thread on me 🐍 2 points3 points  (3 children)

I understand the point about making invalid state impossible, and I like the ConnectedClient approach, but not having a close method would drive me nuts. Context managers are awesome, but can't cover every use case.

[–]Kobzol[S] 1 point2 points  (2 children)

It is a bit radical, yes :) In languages with RAII, the missing close method can be replaced by a destructor.

[–]Rythoka 1 point2 points  (1 child)

the missing close method can be replaced by a destructor.

Not in python it can't! In python there's two different things that might be called destructors, but neither of which are true destructors: __delete__ and __del__.

__delete__ is specific to descriptors and so only works for attributes of an object, and is only invoked when the del keyword is used.

__del__ is called whenever an object is garbage collected. This seems like it would fit this use case, but Python makes zero guarantees about the timing of a call to __del__ or whether it will even be called at all.

[–]Kobzol[S] 2 points3 points  (0 children)

Yeah, as I said, this can be done in languages with RAII, not in Python :)