I previously used __enter__ and __exit__, which I had heard of before. This time, my program development is mainly object-oriented, so I needed to use more methods to achieve some useful features in Python. Here’s a small note.
__len__
After defining it, you can use the len() function on an instance.
1 | def __len__(self): |
__getitem__
After defining it, you can perform indexing and slicing operations on an instance using [].
1 | def __getitem__(self, key): |
__str__
After defining it, when you print an instance using print(), it will print the content returned by this method.
1 | def __str__(self): |
__iter__
After defining it, you can iterate over an object, such as for i in object. Note that this method must return an iterator (not just an iterable).
1 | def __iter__(self): |
__eq__ and __hash__
These two are together because if you want to automatically remove duplicates when placing instances in a set(), both methods need to be defined. Otherwise, the purpose will not be achieved. The __eq__ method makes objects comparable (== and !=, is cannot be used). Note that the other parameter is fixed here; do not change it.
1 | def __eq__(self, other): |
__hash__ then essentially makes objects hashable. The hash value used for hashing will be the one provided by this method.
1 | def __hash__(self): |
That’s it for now. I’ll continue to add more as needed.
```