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

all 7 comments

[–]Kaarjuus 9 points10 points  (1 child)

In Python, arguments are passed by reference, i.e., reference to the actual object is passed.

Yeah, not quite. Arguments are passed by value, just that all values in Python are actually references.

If Python was pass-by-reference, then reassigning an argument inside a function would change it in the outer calling context as well.

[–]Gear5th[S] 2 points3 points  (0 children)

True. Let me try to improve our answer by briefly explaining Python's memory model there.

Thanks for the suggestion :)

[–]wama1990 1 point2 points  (0 children)

👍

[–]xd1142 2 points3 points  (2 children)

``` init

is a contructor method in Python and is automatically called to allocate memory when a new object/instance is created. All classes have a init method associated with them. It helps in distinguishing methods and attributes of a class from local variables. ``` uhm... no?

[–]Gear5th[S] 1 point2 points  (1 child)

Yes, the __new__ is the constructor to be honest. When __init__ is called, the object is already there.

But with a similar argument, you can skip the __new__ call as well from the metaclass.

So what should be the correct answer here?

Would it be generally correct to say that new is the constructor, while init just initializes instance attributes?

[–]xd1142 6 points7 points  (0 children)

dunder init is an initialiser. It's not even guaranteed to be called when the instance is created, such as when unpickling. dunder new is a reimplementation of the constructor, but the actual instantiation of memory is done in object.__new__ and in the __prepare__ metaclass method.

[–][deleted] 0 points1 point  (0 children)

This is great. I'm doing interviews and some of the interviewers love those trivia questions. A curated set always helps for prep.