jbolt jbolt - 2 months ago 8
Python Question

Python Object not callable

I'm new to python:

Created a Class:

class NodeStruct:
"""Struct to hold node data for search trees"""

def __init__(self, name, children, parent_name, edge_weight):
self.name = name
self.childNodes = children
self.parent = parent_name
self.weight = edge_weight

def name(self):
return self.name

def parent(self):
return self.parent

def path_cost(self):
return self.weight

def children(self):
return self.childNodes

def child_keys(self):
return self.childNodes.keys()


Instantiate:

this_node = NodeStruct(start, problem[start], 'root', 0)


The Problem: when I make a call to name()

name = this_node.name()


I get the following error:

TypeError: 'str' object is not callable


Looks like it should be straight forward... What am I missing?

Answer

Which one of the names do you expect this_node.name() to find?

      self.name = name
      # ...

  def name(self):
      return self.name

The solution is likely to change the name of the attribute to self._name:

      self._name = name
      # ...

  def name(self):
      return self._name
Comments