2 ms·
I prefer the Python2 solution actually. If we disregard for a moment the madness of "hasattr" actually invoking the attribute code -- if it does invoke it and a
by alkonaut 11y ago
I prefer the Python2 solution actually. If we disregard for a moment the madness of "hasattr" actually invoking the attribute code -- if it does invoke it and an error occurs, hasn't that proven that the attribute does in fact exist?
Picture a type that has a "t.connect()" function, and a "t.data" attribute. The data attribute will fail if connect() hasn't been called prior to accessing it.
I'd expect hasattr(t, "data") to answer true and never raise an error, regardless of when I do it. The only way to do that (if you have to invoke the code in the attribute -- not sure if that is the case) is to catch and return true.
Validattr would have been a much better name, yes.