In many languages, we use new
to instantiate a new instance of a class. For example, in Java:
class MyClass {
int number = 1;
MyClass(int n) {
self.number = n;
}
}
MyClass obj1 = new MyClass(5);
However, as I've been using Python more and more, I've come to wonder why the new
keyword is even necessary. In Python, we can simply do:
class MyClass:
def __init__(self, n):
self.n = n
obj1 = MyClass(5)
So, what's the purpose of the keyword? Is there some syntactic ambiguity that we resolve by using the keyword?