Accessing attribute data type in PyQGIS

If you need to get the fields of a layer you can use QgsVectorLayer::pendingFields() in Python like so:

fields = layer.pendingFields()

which will give you something like this:

{0: <qgis.core.QgsField object at 0x46d9d40>, 
1: <qgis.core.QgsField object at 0x46d9b00>, 
2: <qgis.core.QgsField object at 0x46d9cb0>, 
3: <qgis.core.QgsField object at 0x46d9ef0>, 
4: <qgis.core.QgsField object at 0x46d9dd0>}

Which you can then go though get the name, data type, and length:

for field in fields.itervalues():
    print "Name:%s Type:%s, Length:%s" % ( field.name(), field.typeName(), field.length())

Name:Seg_Par Type:String, Length:10
Name:Lot_num Type:String, Length:5
Name:Plan_num Type:String, Length:10
Name:Tenure Type:String, Length:2
Name:Area_ha Type:Real, Length:14

For QGIS 3 users:

The function pendingFields() has been replaced by fields()

for field in layer.fields():
    print( "Name:%s Type:%s, Length:%s" % ( field.name(), field.typeName(), field.length()))