0

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?

1
  • you're overwriting the method in the constructor self.name = name Commented Sep 17, 2016 at 21:23

2 Answers 2

4

When you define your class, name is a function. As soon as you instantiate it, though, __init__ is called, and name is immediately set to whatever you pass in (a string in this case). The names of functions are not kept separate from the names of other objects. Use a unique name.

Sign up to request clarification or add additional context in comments.

1 Comment

You were first to answer but the code example from @ev-br did help me to understand it somewhat more but I will uptick because I understand what you are getting out.
2

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.