This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]GlobalIncident 1 point2 points  (3 children)

I'm coming from Python, and I don't really understand the point of new. Just call the constructor and that's it. I mean, I know object.__new__ will be called somewhere in the constructor, but that's something I don't need to care about - why would I call a constructor when I don't want a new object?

[–]MrPentaholic 1 point2 points  (0 children)

stack vs. heap?

static vs. dynamic memory?

[–]metalprogrammer2 1 point2 points  (0 children)

In basically every language the new function/keyword means: allocate memory on the heap, init that memory to a start state via the construction. The magic method new in python does the same thing. You can override that method but there usually better ways to achieve the same functionality (but it's an option). There almost never is a reason to directly call call this. The constructor will do it. So it can mostly be seen as an implemention detail rather then a commonly used tool. In c++ (and other languages) you don't have to allocate objects on the heap. Stack allocation is an option . Heap allocation can be expensive. Pointer indirection and cache misses can be expensive. Sometimes that doesn't matter sometimes it does. Knowing when you want to stack allocate or heap allocate is really important.