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
Post a Comment