7 comments

[ 3.8 ms ] story [ 19.6 ms ] thread
I was happy to see some Python tips I actually haven't already seen. That was a good, short, interesting read. Thanks for writing it up!
I'd heard of __slots__ long ago but I didn't know enough to use it. The example is good!
A couple of interesting capabilities in this post. The section "Controlling What Can Be Imported and What Not" is wrong or misleading though. The dunder __all__ only limits wildcard imports like `from my_module import * `. In your example both `foo` and `bar` can be imported/accessed like `from my_module import foo, bar` or `import my_module; my_module.foo(); my_module.bar()`.

Edit: Also just tried to `import * ` with `_all__ = []` and no AttributeError is raised as is stated. Python 3.7.4

Wow thank you very much MartinHeinz -- I've been using Python for years and the only two of these I knew were the __enter__ and comparator dunders! Saved, bookmarked, and shared with friends.
Did not know about the kwarg only function. Neat. Although I wonder what it means for readability esp if the idiom is not well known.

A general observation is that the itertools module is full interesting nuggets. I am always finding something new.