Recently I was reading about magic methods in Python, which make the code a lot easier to read. Can we define our own mappings? If so, is there any pointer for this and how complicated it would be?
For example, +
is always mapped to __add__
. Could I define a mapping for ?
, which would call __is_valid__
in my class?
c = Car()
print(c?) # invokes __is_valid__ of Car to get the result