1

The code below is for constructing a linked list.

I created an instance of Node named a, and gave it an item 'hello', and created an instance of a Node named b, and then gave it a value 'world', and then did a.setOther(b);

When I then type a.getOther() I don't get 'world', but I get <__main__.Node at 0x140ae7fafd0>.

Is this like a pointer, giving the location of the data on my disk? If so, how do I then display the data from this location?

 class Node(object):
    def __init__(self, item, other = None):
        self.item = item
        self.other = other

    def getItem(self):
        return self.item

    def getOther(self):
        return self.other

    def setItem(self, item):
        self.item = item

    def setOther(self, other):
        self.other = other
Md Sifatul Islam
  • 846
  • 10
  • 28
teddyv
  • 29
  • 1
  • 6
  • 1
    The first part of the question is a duplicate. The second part might be as well, though not as obviously. You need to use `a.getOther()` rather than `a.getOther` if you want to invoke the method -- but why bother with getters and setters? It is dated, but the following is worth a read: http://dirtsimple.org/2004/12/python-is-not-java.html Or this: http://www.python-course.eu/python3_properties.php – John Coleman Feb 12 '17 at 13:01
  • Ok, thanks. I'm going to edit the post to only the second half of the question. I also noticed I made an error - I meant to say `a.getOther()` not `a.getOther` – teddyv Feb 12 '17 at 13:20
  • You're getting `<__main__.Node at 0x140ae7fafd0>` because the `Node` class doesn't define a `__str__` or `__repr__` method, so something generic is used. – martineau Feb 12 '17 at 14:06
  • @teddyv hope this solves your problem! http://stackoverflow.com/a/42191677/6840615 – Md Sifatul Islam Feb 12 '17 at 18:47

2 Answers2

0

This isn't an issue, really, the code works okay and you can verify that getOther does the right thing by a simple equality:

a.getOther() == b
True

the issue is that when you execute a.getOther() the interpreter will call the __repr__ of the object returned to print it out. If you need a custom representation, you'll need to specify it by adding a __repr__:

def __repr__(self):
   return "Node({0}, {1})".format(self.item, self.other)

the output of __repr__ should ideally be similar to the statement you'd use to construct the Node object.

Similarly, you'll need to define a __str__ if you print an object using print and wanted a more user friendly representation for the user.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
0

I created an instance of Node named a, and gave it an item 'hello',and created an instance of a Node named b, and then gave it a value 'world',

a = Node('hello')
b = Node('world')

and then did a.setOther(b);

a.setOther(b)

When I then type a.getOther() I don't get 'world'

setOther(b) is storing a reference of Node b; when a.getOther() is called (in your code)you simply returned return self.other which is the address of the reference; whereas you needed the item inside the Node b. so you should have instead returned return self.other.item

Try this:

def getOther(self):
   return self.other.item
Md Sifatul Islam
  • 846
  • 10
  • 28