In Python I'm making a graph class which I intend to design by creating a Node class, and then an Edge class which takes as arguments to the constructor two nodes, a source and target. I'd also like to check in the Edge class that the two inputs are in fact Nodes, so I'd like to include some lines like
assert type(src) is Node and type(tgt) is Node However, this will not act as I expect it to. As I understand it, I would need to use something like src.__name__ is Node.__name__ but this just seems ugly. Is there a way to control the type name of a user-defined class so that the code reads a little more nicely?
I tried following some of what I found here: Python: Change class type name
and wrote the code
class Node: ... __class__ = type("Node", (type,), {}) I'm not sure if I was mis-applying the lessons found in that link or if those lessons wouldn't accomplish what I'm trying to do--but one way or another, it didn't do what I was hoping for.
type(src) is Nodework?assert type(src) is Node and type(tgt) is Nodeshould work, how does it not work for you?