I'm trying to define a class which takes in a default parameter for its init function. I've defined a class as follows:
class Node:
def __init__(self,name,visited=False,distance=math.inf,path=Node('-')):
self.name = name
self.visited = visited
self.distance = distance
self.path = path
and I get the following error:
NameError: name 'Node' is not defined
I was able to get around this problem by "pre-defining" the parts of the class that I needed, like so:
class Node:
def __init__(self,name):
self.name = name
class Node:
def __init__(self,name,visited=False,distance=math.inf,path=Node('-')):
self.name = name
self.visited = visited
self.distance = distance
self.path = path
but I can't shake the feeling there's a better, more pythonic way.
pathbe for theNodeobject you'd be setting as the default?Nodeobject? What does the firstNodeto ever exist point to, when there don't exist any otherNodes yet?SENTINEL = object()