Follows,

I need some help with fixing a bug in my program. I can't post the entire code here as its an on going assignment.

I'm building a Tree in Python.

class Tree:
	def __init__(self, f, r):
		self.first = f
		self.rest = r

This is my Tree.

I perform a depth-first search on it.

So I call

traverse(tree.rest)

The traverse function looks like this:

def traverse(vertex):
	if len(vertex) == 0:
		return
...

The following is the error that keeps poping out:

if len(vertex) == 0:
AttributeError: Tree instance has no attribute '__len__'

Can someone help me with this.

drjay

traverse(tree.rest)

tree would have to be an instance of the Tree class for this to work.

class Tree:
	def __init__(self, f, r):
		self.first = f
		self.rest = r 
		
tree = Tree(1, [])
vertex = tree.rest
print "tree", 
if len(vertex) > 0:
   print "Greater"
else:
   print "Lesser/Equal"
   
tree_2 = Tree(1, [123])
vertex = tree_2.rest
print "tree_2",
if len(vertex) > 0:
   print "Greater"
else:
   print "Lesser/Equal"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.