by wdroz · 11mo ago · 0 comments
...
print(container.area)
> AttributeError: 'Container' object has no attribute 'area'. Did you mean: 'inner.area'?[0] -- https://docs.python.org/3.15/whatsnew/3.15.html#improved-err...
Search is limited to 20 attributes and non-descriptors only to avoid arbitrary code execution.
I assume constructing AttributeErrors isn't highly performance sensitive.
> using exceptions for control flow is deprecated?
Exceptions are for the exceptional cases - the ones that mean normal operations are being suspended and error messages are being generated. Don't use them for control flow.In Python, the VM raises a KeyboardInterrupt exception when the user hits ctrl+c in order to unwind the stack, run cleanup code and eventually exit the program.
Python is a quite heavy user of exceptions for control flow.
I really dislike this too, but that’s how it is.
On the other hand it's not like Python really cares about performance....
[heavy green check mark]
try:
data = collection['key']
except KeyError:
data = ..try something else..
[red x] if 'key' in collection:
data = collection['key']
else:
data = ..try something else..
The latter form also has the genuine disadvantage that nothing ensures the two keys are the same. I've seen typos there somewhat often in code reviews.https://realpython.com/python-lbyl-vs-eafp/#errors-and-excep...
for key in possible_keys:
if key in collection:
...
is fine and isn’t subject to your disadvantage. data = collection.get("key")
if data is not None:
...
else:
.... data = collection.get("key")
if data:
...
else:
...