Why some builtin methods in python requires assigning to a variable? -


i confused python treats below examples.

example 1

   >>> = '{}'    >>> a.format('1')    '1'    >>>    '{}'    >>> 

example 2

>>> = [] >>> a.append(1) >>> [1] 

in example 1, 'a' '{}' in example 2, 'a' [1]

in both examples, i'm not assigning result 'a', what's difference?

answering on broader sense require more detail study particular case difference crops because 1 of object mutable , other non-mutable.

you cannot expect call method on string , change it. supposed create new string object.

for list.append, because mutable, method call can update list in place.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -