2

I'm trying to define a class Hlist of linked lists as below:

class Hlist:

def __init__(self, value, hlnext):
    self.value = value
    self.hlnext = hlnext

def pop(self):
    res = self.value
    if not(self.hlnext == None):
        self = self.hlnext
    return res

def __repr__(self):
    return (str(self.value) + ' - ' +  str(self.hlnext))

When I test the pop() method on

a = Hlist(1, Hlist(2, None))

Python returns 1 - 2 - None, ok. Then

a.pop()

returns 1, fine. However :

print(a)

returns 1 - 2 - None. The list hasn't been modified despite

self = self.hlnext

Is self the pointer a or is it another pointer pointing to the same address as a? And why does the following code return the expected answer for pop():

class Hlist:

def __init__(self, value, hlnext):
    self.value = value
    self.hlnext = hlnext

def pop(self):
    res = self.value
    if not(self.hlnext == None):
        self.value = self.hlnext.value
        self.next = self.hlnext.hlnext
    return res

def __repr__(self):
    return (str(self.value) + ' - ' +  str(self.hlnext))

is it due to the setattr function used by python?

Actually i was trying to get the equivalent in Python of the following class in Java :

class Hlist{
    int value;
    Hlist hlnext;

    Hlist(int value,Hlist hlnext){
        value = value;
        hlnext = hlnext;
    }
}

and add a pop() method to it. In a pop() method, will Java's this work the same way Python's self does (local variable) or will it be binded to the pointer a I called pop()? In that case, will this = this.hlnext change the a pointer or not?

H.M.
  • 261
  • 3
  • 8

2 Answers2

1

Because self isn't working the way you think it is. self is just another local variable: assigning to it inside pop() won't change the object into another thing. See this question for more details.

Community
  • 1
  • 1
kfb
  • 6,252
  • 6
  • 40
  • 51
  • Ok, I just upated my post to respond to your answer, does Java `this` work the same way as Python `self` ? – H.M. Sep 28 '16 at 14:51
  • I'm not a Java expert, but a quick search suggests that [assigning to `this` in Java is completely forbidden](http://stackoverflow.com/a/7979791/246534). – kfb Sep 29 '16 at 10:43
0

It's moslty because you can't change self directly.
If you think about pointers, you can't change the pointer address, except if you use a pointer on this pointer. Here, if you consider self as a pointer, when you assign another value to self, you don't really change the self pointer.

See this answer

The second code "works" (not in all cases), because you aren't changing self itself, but the references on which it's pointing. Then your instance is updated to remove its old value and update itself with the next value.

Community
  • 1
  • 1
romain-aga
  • 1,441
  • 9
  • 14
  • So actually, `self.hlnext` is a pointer that exists outside of the function (in the instance) whereas `self` is a copy of `a`? – H.M. Sep 28 '16 at 14:49
  • In python we talk about reference as java. So `self.hlnext` is a reference that exists outside the function, as long as you are using an instance to get it. `self` is not really a copy of `a`, it's `a`, but you can't modify its reference. (If you dig a bit about metaprogramming, you will see that you can call any instance methods in python by passing the `self` argument yourself.) – romain-aga Sep 28 '16 at 14:57