I guess the question is whether you're writing for the general case or your own database. In the general case, such as a script that you'd post here in the script repository, you need to check that an attribute exists, as Mario demonstrated. There's a lot more checking and less assumption involved.
I was writing for my own database, so I know what the attribute set is called and what each attribute is called, and all I was looking to do was check that an attribute record had already been created for each image, and if not, then create one, and finally use it to either read or write an attribute.