I am starting to learn Python from Java. I would like to write Python code similar to this line in Java:
public BinaryNode(T nodeData, BinaryNode<T> leftChild, BinaryNode<T> rightChild) {}
Here is what I have tried so far:
from typing import TypeVar
T = TypeVar('T')
class BinaryNode:
def __init__(self, nodeData : T, leftChild : BinaryNode = None,
rightChild : BinaryNode = None):
self.nodeData = nodeData
self.leftChild = leftChild
self.rightChild = rightChild
How I should specify that the BinaryNodes passed to __init__
should have the same type parameter as the nodeData
? Thanks for your help.