1

How do we know when to reassign to a variable after performing some method to an input?

For example, for stringg = "Johnathan" if we do stringg.split() then we would need to reassign this, otherwise stringg is still == "Jonathan"

However, if we have nums = [1,2,3] and do nums.reverse(), we don't have to reassign the latter and if we print nums, we get [3, 2, 1]

I am quite confused as to when we need to reassign after performing an operation, and when we do not need to.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
ni odeh
  • 35
  • 3
  • 4
    Check the documentation if a function returns a value or does a so called in-place operation. – Klaus D. Jun 29 '21 at 21:14
  • 1
    It comes as you gain experience. Until then you either have to read each method docs to see if it is in place, or use a debugger. A good rule of thumb is that all string methods are not inplace since strings are immutable. – DeepSpace Jun 29 '21 at 21:14
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). Stack Overflow is not intended to replace existing documentation and tutorials. You simply read the documentation for the function, to see whether it returns a new object, or alters the given argument "in place". – Prune Jun 29 '21 at 21:15
  • IMO, Python and many of its packages are woefully inconsistent about whether functions return values or operate by side-effect (or both). While it's true that it's all there in the doc.s, this inconsistency means I have to slow down my work to refer to the doc.s – Mark Lavin Jun 29 '21 at 21:18
  • This is a place where some LISPs got it right, by having a naming convention for functions that do things in-place (`split` would return a value without changing the original, `split!` would change the original and not return any value). – Charles Duffy Jun 29 '21 at 22:19

2 Answers2

2

stringg = "Johnathan" || stringg.split() returns an array, so it is a function that gives something back from an input (in this case, "Johnathan"). Therefore, a new variable needs to be created to store whatever is returned by split()

nums.reverse on the otherhand is a Modifier method. It takes an input and changes the input, but doesn't return any value based on the input.

The documentation for each method will give better insight as to whether it provides a return value or not.

Split

Split Official Documentation

Reverse

Reverse Official Documentation

EDIT @DeepSpace 's comment is worth remembering as well:

A good rule of thumb is that all string methods are not inplace since strings are immutable

StarshipladDev
  • 1,166
  • 4
  • 17
  • 1
    Linking to the official Python docs would be better (IMHO): str.split - https://docs.python.org/3/library/stdtypes.html#str.split, list.reverse - https://docs.python.org/3/tutorial/datastructures.html#more-on-lists – Gino Mempin Jun 29 '21 at 21:46
  • @GinoMempin Edited as such – StarshipladDev Jun 29 '21 at 21:50
1

Firstly, I think part of your confusion is what exactly "variable" means. In Python it's not the best terminology because Python really has names (or identifiers), and values (or objects). Names point to values. For more on that, see Facts and myths about Python names and values by Ned Batchelder.

Secondly, another part of your confusion might be the difference between mutable and immutable types. Strings are immutable, but lists are mutable. Therefore strings can't have methods that mutate them, but lists can. (And for that matter, other functions can mutate them, e.g. random.shuffle(nums).) For more on that, this answer is a good intro.

Now, even with that out of the way, there's still some ambiguity, but the function documentation will almost always tell you. And if it helps, the difference here is between procedures, which are used for their side-effects*, and functions proper, which are used to return a value. See What is the difference between a "function" and a "procedure"? But these are just concepts; the reality is a grey area; for example the file method write() is primarily used to write to the file, but it also returns the number of characters written.

Related concepts:


* Sidenote: "Mutator method" would seem like a natural fit, but that has a more specific definition in the context of OOP.

wjandrea
  • 28,235
  • 9
  • 60
  • 81