I have just spent several frustrating hours because I was searching the api docs for stuff on attributes when I should have been looking for fields.
Can someone please explain what the difference or relationship is?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
In Python an attribute defines an object variable, which may be accessible through the dot-notation, like
object.attribute. Using a wrong name for an attribute throws a specific exception, AttributeError.
Now pyqgis defines a method called attribute, which takes a name of a field and returns the value of the feature for that field (https://qgis.org/api/classQgsFeature.html#ae5957f3cf812dc7c077ee7f906745ac7). Also it defines QgsAttributes, which is implemented as a Python list of Python objects (https://qgis.org/api/classQgsAttributes.html) and so is a more general class.
So it seems, that an attribute is feature-related and has a name and a value, where a Field defines the logical part of a table, with name, data type and so on. I don't know if this is the way it's ment to be by the autors of the QGIS-API, but in addition to that, the python-API is directly derived from the C++-API, where other naming conventions exist, which then collide with python and QGIS-Terms. So at least consistency is not easy to keep.