I'm trying to pass by object a None
value in hopes of reassigning it to an actual value. Can someone please explain why this isn't working? I thought since None
is a NoneType
that it would be passed in by reference and when I use the passed in variable, I would alter the original None
object. Is this incorrect? I have provided a snippet below to demonstrate my case:
class MyClass: def __init__(self): self.value = None def assign(self, object): object = OtherClass()example = MyClass()example.assign(example.value)
The result is that example.value
is still None
. Why is that? I have read a some SO posts but none of them are clear in explaining this.
EDIT: Not a duplicate because I wanted to know why it was behaving like pass by value when I thought it should be pass by reference. In the end, I have concluded that NoneType
objects are immutable and therefore always pass by value. I would need to use list
type object or something mutable.
EDIT again: My example code is just a general case. I was trying to implement a BST and my initial root node was None
and when I assigned the root node to be a Node
object, it would still be NoneType
, which caused me to be confused and was the cause of this question.
Last edit: answer below provides a very good tl;dr summary of pass by object. I was unable to understand just by searching/reading forums.