I am trying to extend some concepts in the QGIS Workshop tutorial. Basically, I would like to check if the layer is of type "QGSVectorLayer". In the QGIS Python console, I am able to type

canvas = qgis.utils.iface.mapCanvas()
cLayer = canvas.currentLayer()

with no problem. However, when attempting the same from the plugin, I get the following error message:

if type(self.cLayer).name == "QgsVectorLayer": NameError: global name 'cLayer' is not defined

The code is as follows:

def handleLayerChange(self, layer):
        self.cLayer = self.canvas.currentLayer()
        if self.cLayer:
            if type(self.cLayer).__name__ == "QgsVectorLayer":
                self.provider = self.cLayer.dataProvider()

2 Answers 2


You don't need to compare it using strings or the name of the type. You should just do:

 layer.type() == QgsMapLayer.VectorLayer
  • Thanks, this is much better. I did end up getting the other method to work by converting it to a string; if str(type(self.cLayer).__name__) == "QgsVectorLayer" May 9, 2013 at 22:52
  • The pythonic, recommended way is using isinstance as suggested in @KitKat's answer! Feb 8 at 9:49

Another way is testing against the desired class type, like this:

from qgis.core import QgsVectorLayer

# ...
if isinstance(layer, QgsVectorLayer):
    # ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.