Is using magic methods in high level Python code is allowed by PEP8 ? For example, does PEP8 allows for:
ab = {'a', 'b'}
ab.__len__()
?
I often write code in editors when it's much easier to write left to right without going back to the beginning of a line.
Calling __len__
directly doesn't violate PEP8, however, should be avoided as the inbuilt functions that use the magic methods do additional checks.
Example:
class Test:
def __len__(self):
return "hi"
print(Test().__len__()) # Prints hi
print(len(Test())) # Prints "TypeError: 'str' object cannot be interpreted as an integer"