このコードはPythonであり、正常に機能します。
# tests if class exists in the dictionary attrs
try:
self.attrs['class']
# if it doesnt python will throw an exception
except KeyError:
self.attrs['class'] = "someclass"
# else it is defined, so we concat someclass to previous value
else:
self.attrs['class'] = "someclass %s" % self.attrs['class']
ただし、これは良い習慣ではないのではないかと思います。Pythonに更新があり、スローされた例外の名前が変更された場合、動作が停止する可能性があるためです。それは悪い習慣ですか?それを行うためのより良い方法はありますか?