-4

Here in this code they are checking the equality of self with self.parent.leftChild . why they are doing this

def isLeftChild(self):
    return self.parent and self.parent.leftChild == self

The site is:-class TreeNode: http://interactivepython.org/runestone/static/pythonds/Trees/SearchTreeImplementation.html

Rathi_Ji
  • 29
  • 9
  • 1
    1. `self` is not an operator. 2. What do you mean *"how"*? 3. It's not clear which value you're referring to as which. Is it the formulation `foo and foo.bar == foo` that you find surprising? It's not actually checking equality with two parameters, it's checking first that `self.parent` is truthy (probably `not None`) and secondly that its `leftChild` attribute is equal to `self`. – jonrsharpe Jun 14 '16 at 12:40
  • here they are doing self.parent and self.parent.leftChild == self i want to know what do they meant by doing this – Rathi_Ji Jun 14 '16 at 12:42
  • 1
    `(self.parent is not None) and (self.parent.leftChild == self)` is probably a less confusing formulation, does that make more sense? – jonrsharpe Jun 14 '16 at 12:42
  • 1
    `self` is not an operator, it is a reference to the object for which a method is called. If `node.leftChild()` is invoked, `self == node` _inside_ the `leftChild` code. In other languages, like Java or C++ or JavaScript, the same thing is called `this`. [Read more](https://docs.python.org/3/tutorial/classes.html). – 9000 Jun 14 '16 at 12:43
  • what is there in self i mean i have seen the things like a=1 and then we can do return a==1 – Rathi_Ji Jun 14 '16 at 12:46
  • 1
    See e.g. http://stackoverflow.com/q/2709821/3001761 for more explanation of `self`. As a starting point, note that it's *just a parameter of the method*. – jonrsharpe Jun 14 '16 at 12:47
  • sir can you tell me what is the value that is in the self argument with which they are comparing self.parent.leftChild – Rathi_Ji Jun 14 '16 at 13:02
  • 1
    `self` is the instance of the class the method belongs to. If you're not familiar with classes you should read the [tutorial](https://docs.python.org/3/tutorial/classes.html) first. – Matthias Jun 14 '16 at 13:06

1 Answers1

4

Here in this code they are checking the equality of self with the other two parameters.

What? Where? No. a and b == c doesn't relate a and c together in any way.

self.parent and self.parent.leftChild == self

checks if

  1. self.parent has a meaningful value (is not None) and, if so,
  2. self.parent.leftChild equals to the given self.

In other words, it does what its name says: it checks if "we" are identical to our parent's left child. This, of course, only works if we have a parent. If we haven't, we aren't its left child.

Note: What I just said about "is not None" is only half the truth. To be exact, it checks if there is a "truthy value", i. e. a value which evaluates true in the context of conditional expressions. As one normally uses None in the case of an absent node such as parent, what I wrote is clear enough.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • sir can you please explain the second point – Rathi_Ji Jun 14 '16 at 12:54
  • 1
    "Sir" sounds a bit ironical onplatforms like these. Anyway, imagine you are an object in a tree, in a kind of parent/child relationship. In this structure, every child knows its parent, and every parent knows its (left and right) childs. So here we ask about our parent (`self.parent`) and ask this parent about its left child (`self.parent.leftChild`). If this one is identical to ourselves, we found out that we are the left child of our parent. – glglgl Jun 14 '16 at 12:57
  • Yes I started Getting you , but again they are doing (==self)what is stored in (self) and also in the link that i have mentioned they in the same class they are in the replaceNodedata method they are even putting the value of self (self.leftChild.parent = self) it confuses me – Rathi_Ji Jun 14 '16 at 13:08
  • Might be slightly more accurate to say that `self.parent` checks if it has a truthy value instead of `not None` – Vincent Savard Jun 14 '16 at 13:26
  • @VarunRathi I am not sure what exactly confuses you. Is it just the lack of the basics? An explanation what is `self` can be found directly on the right of the question, there are links to other questions about the same topic ("Linked", "Related"). Besides, there is a very fine tutorial at the python doc site. (You were pointed to that by other commenters directly below your question.) Essentially, `self` holds a reference to the object just worked on, the object whose method just has been called. And `xy == self` compares if the variable `xy` refers to the same object as `self`. – glglgl Jun 14 '16 at 13:32