3

I'm new to Python and object orient programming, and have a very basic 101 question:

I see some methods return a modified object, and preserve the original:

In: x="hello"
In: x.upper()
Out: 'HELLO'
In: x
Out: 'hello'

I see other methods modify and overwrite the original object:

In: y=[1,2,3]
In: y.pop(0)
Out: 1
In: y
Out: [2, 3]

Are either of these the norm? Is there a way to know which case I am dealing with for a given class and method?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Lee88
  • 1,185
  • 3
  • 15
  • 27
  • 2
    Methods of mutable objects don't "overwrite" the original object -- they mutate them. Both mutable and immutable objects are central to Python. Any basic introduction to Python should explain the difference. Stack Overflow isn't designed to reproduce information which is readily available elsewhere. – John Coleman Nov 21 '16 at 01:39
  • 1
    To know what the case is with a specific class/method, just looking at the docs is a good way to find out. They're used in different situations. – qxz Nov 21 '16 at 01:41
  • 1
    See [Immutable vs Mutable types](http://stackoverflow.com/questions/8056130/immutable-vs-mutable-types) and the links to the official Python docs there. – PM 2Ring Nov 21 '16 at 01:42
  • An immutable object cannot be modified, but of course it can be replaced. The usual Python convention is that functions & methods that operate on a mutable object perform the operation in-place and return `None`, they don't return the modified original. OTOH, there are some functions that behave like the `sorted` function, which can be given a mutable or immutable collection to sort, but it always returns the result in a new list and doesn't modify the original even if it is mutable. Intenaly, `sorted` creates a new list from the input collection, sorts that new list in-place, and returns it. – PM 2Ring Nov 21 '16 at 01:49
  • 2
    Especially see [abarnert's excellent answer](http://stackoverflow.com/a/29604031/4014959) at the question I linked above. And you may also find this article helpful: [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder. – PM 2Ring Nov 21 '16 at 02:08

2 Answers2

5

Your examples show the difference between immutable built-in objects (e.g., strings and tuples) and mutable objects (e.g., lists, dicts, and sets).

In general, if a class (object) is described as immutable, you should expect the former behavior, and the latter for mutable objects.

cco
  • 5,873
  • 1
  • 16
  • 21
4

Both of these are idiomatic in Python, although list.pop() is a slightly special case.

In general, methods in Python either mutate the object or return a value. list.pop() is a little unusual in that, by definition, it must do both: remove an item from the list, and return it to you.

What is not common in Python, although it is in other languages, is to mutate an object and then return that same object - which would allow for methods to be chained together like so:

shape.stretch(x=2).move(3, 5)

... but can cause programs to be harder to debug.

If an object is immutable, like a string, you can be sure that a method won't mutate it (because, by definition, it can't). Failing that, the only way to tell whether a method mutates its object is to read the documentation (normally excellent for Python's built-in and standard library objects), or, of course, the source.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160