Have you tried defining __repr__? Joseph
On Aug 29, 2013, at 7:56 PM, Maria McKinley <[email protected]> wrote: > Thanks Morris. That does answer one question, I can't assume code on > wikipedia is bug-free. Changing it doesn't solve the problem, unfortunately, > but you are right, time to hit the debugger. Thanks everyone. > > cheers, > Maria > > > On Thu, Aug 29, 2013 at 5:46 PM, Morris Bernstein > <[email protected]> wrote: > I hate to suggest this because I almost never use it, but have you considered > using the pdb debugger and setting a breakpoint? > > Meanwhile, your problem is here: > def items(self): > """Return an iterator over the items of the `Trie`.""" > for char, node in self.root.iteritems(): > if node.value is None: > yield node.items > > node.items is the the Trie.items() method bound to the node object. > > I think, taking a quick look at the code, you want to yield node.items(), > function call again. Looks like the same problem. > > > > > > > > On Thu, Aug 29, 2013 at 5:17 PM, Maria McKinley <[email protected]> > wrote: > Doh. Thanks. This does the trick, but it gives me the instance location. I > assumed this is because there is no __str__ method defined, but when I added > a __str__ method it didn't change anything. Probably didn't implement the > __str__ method correctly, but since I didn't even get an error, not sure this > was even the problem. (Pretty sure, for example, that I shouldn't always be > referencing the head node.) > > def __str__(self): > return "Node letter is %s" % (self.root[0]) > > for c in mytrie.items(): > print c > ...: > <bound method Trie.items of <trie.Trie instance at 0x1010dc710>> > <bound method Trie.items of <trie.Trie instance at 0x1010dca70>> > > thanks again, > Maria > > > On Thu, Aug 29, 2013 at 4:40 PM, Cris Ewing <[email protected]> wrote: > I expect that the problem here is that you are attempting to iterate over the > method itself, rather than its result. You'd need to call the method to do > that: > > for c in mytrie.items(): > print c > > hth > > c > > On Aug 29, 2013, at 4:38 PM, Maria McKinley wrote: > >> Hello, >> >> I hope someone on this list doesn't mind answering what I think is a quick >> question. I have been playing around with the python code found here: >> >> http://en.wikipedia.org/wiki/Trie#A_Python_version >> >> I can't get the iterator to work, and I wonder if I'm not calling it >> correctly. I thought once I made my object, and added stuff to it, I could >> just do this: >> >> for c in mytrie.items: >> print c >> >> but I get this error: >> >> TypeError: 'instancemethod' object is not iterable >> >> What am I doing wrong? >> >> thanks, >> Maria > > Cris Ewing > -------------------------------------------------- > Principal, Cris Ewing, Developer LLC > http://www.crisewing.com > [email protected] > 1.206.724.2112 > > > > > -- > Maria Mckinley > Software Developer with Bonus SysAdmin Experience > www.mariakathryn.net > www.linkedin.com/in/mariamckinley > > > > > -- > Maria Mckinley > Software Developer with Bonus SysAdmin Experience > www.mariakathryn.net > www.linkedin.com/in/mariamckinley
