Quick note on Python syntax magics

As noted in Python 3 documention, behaviors of +, -, *, etc. can be redefined.

Specially, I would like to take notes on some special yet common methods.

__repr__ method

This method is called when you apply print on the instance. Could be useful when debugging class related problems.

__enter__, __exit__ methods

Those two methods are key components of a context manager. Refer to https://jeffknupp.com/blog/2016/03/07/python-with-context-managers/ to get a sense on how to manage the fragile resouces with context manager.


发表评论

电子邮件地址不会被公开。