Which class method is being called when we declare below expression?

Ben Finney ben+python at benfinney.id.au
Fri Sep 28 02:29:09 EDT 2018


Ajay Patel <ajay.patel305 at gmail.com> writes:

> L = [1,2,3]

That's not an expression; it is an assignment statement.

The right-hand side is an expression. It will (at the top level) create
a list.

To create a new instance of the 'list' type, Python will call the type's
'__new__' method. This is termed the constructor for that type.

The constructor returns a new instance of the type; in this case, it
returns a new instance of 'list'. That object is the result of
evaluating the right-hand side of the expression.

The statement then assigns the reference 'L' to that object.

> And
> L =[]

All the above description also applies to that assignment statement.

-- 
 \        “If you go parachuting, and your parachute doesn't open, and |
  `\        you friends are all watching you fall, I think a funny gag |
_o__)             would be to pretend you were swimming.” —Jack Handey |
Ben Finney




More information about the Python-list mailing list