I'm having trouble translating the following JavaScript code into python. My problem is inheriting the parent properties and methods in the ChildNode class by using super.__init__()
. In js, you just call super()
and get all the props. It doesn't seem to be the same in python. The super function is demanding to have val and key inside the parenthesis but I'm not sure how that would work because Im getting val from the ChildNode.__init__
function
JS CODE that does what I want
class BinarySearchTree {
constructor(val, key) {
this.val = val;
this.left = null;
this.right = null;
this.key = key;
}
insert(val) {
const currNode = this;
if (val === currNode.val) return null;
if (val < currNode.val) {
if (!currNode.left) {
currNode.left = new ChildNode(val);
return this;
} else {
currNode.left.insert(val);
}
}
if (val > currNode.val) {
if (!currNode.right) {
currNode.right = new ChildNode(val);
return this;
} else {
currNode.right.insert(val);
}
}
}
}
class ChildNode extends BinarySearchTree {
constructor(val) {
super();
this.val = val;
delete this.key;
}
}
var root = new BinarySearchTree(20, 14);
root.insert(8);
root.insert(22);
root.insert(4);
console.log(root);
Python attempt
class BinarySearchTree:
def __init__(self, val: int, key: int):
self.val = val
self.left = None
self.right = None
self.key = key
def insert(self, val):
currNode = self
if val == currNode.val:
return None
if val < currNode.val:
if not currNode.left:
currNode.left = ChildNode(val)
return self
else:
currNode.left.insert(val)
if val > currNode.val:
if not currNode.right:
currNode.right = ChildNode(val)
return self
else:
currNode.right.insert(val)
# having trouble inheriting here
class ChildNode(BinarySearchTree):
def __init__(self, val):
super().__init__()
self.val = val
del self.key
root = BinarySearchTree(20, 14)
root.insert(8)
root.insert(22)
root.insert(4)
print(root)
CodePudding user response:
In Javascript if you do not pass parameters to a function it defaults them to being undefined
, however, this is not tolerated by Python. If you run your code you will notice you get the following error.
Traceback (most recent call last):
File "main.py", line 41, in <module>
root.insert(8)
File "main.py", line 17, in insert
currNode.left = ChildNode(val)
File "main.py", line 34, in __init__
super().__init__()
TypeError: __init__() missing 2 required positional arguments: 'val' and 'key'
Which is very helpful because it tells us that the super.__init__()
call is missing two arguments, which are defined in the super class.
If you intend to replicate the JS behaviour then you would do well to add a default None
to the arguments in the constructor of the superclass.
class BinarySearchTree:
def __init__(self, val: int = None, key: int = None):
self.val = val
self.left = None
self.right = None
self.key = key
CodePudding user response:
The child class's __init__
method needs to pass the appropriate arguments in the super().__init__()
call.
# having trouble inheriting here
class ChildNode(BinarySearchTree):
def __init__(self, val: int):
super().__init__(val, '')
del self.key
You don't need to do self.val = val
in the child class, since that's done by the parent class.
That said, this doesn't seem like an appropriate class hierarchy in either language. You shouldn't delete attributes in the child class, because any inherited methods that depend on it will fail.