0

I used a graph package coinor.gimpy to implement some algorithms in network theory. And I want to use the graph class as one input of a function, however, I want to check whether this variable is this class or not, I don't know how to do, following are some of the code.

from coinor.gimpy import Graph    
g=Graph()
print type(g)

and the result is

<class 'coinor.gimpy.graph.Graph'>

Then inside one function, the code might be like the following,

def Dijkstra(g)
    if type(g) == ??
        then ...

my question is: what should I write to replace the question mark? Thanks.

user207421
  • 305,947
  • 44
  • 307
  • 483
ilovecp3
  • 2,825
  • 5
  • 18
  • 19

1 Answers1

0

The most common method is to use isinstance:

isinstance(g, Graph)

This will return True or False depending on whether or not g is an instance or subclass of Graph.


An alternate approach is to use type and the is operator:

type(g) is Graph

This will return True or False depending on whether or not g is of type Graph.