2

I know there are things to avoid repetition, like:

# Long way
num = num + 10

# Short way
num += 10

But when talking about string methods for example. Is there a short way to avoid that?

myString = myString.upper()
ce0
  • 29
  • 1
  • 1
    Why, specifically, would you want to make it shorter? It's clear and explicit. – NPE Jan 02 '17 at 22:49
  • How *would* that work? `myString.upper()=`? `myString=.upper()`? `myString.upper=()`? – jonrsharpe Jan 02 '17 at 22:50
  • no, there is no short method. Some modules like `pandas` has functions which have option `in place` and they can do something like`df[0].some_function(inpalce=True)` but standard string doesn't have `in place` option. – furas Jan 02 '17 at 22:50
  • 1
    I'd like to make it shorter for the same reason that operation sign before equal exists. To avoid type the name of the variable over and over again when I want to make a change to it – ce0 Jan 02 '17 at 22:53
  • I'm still learning python, then I just went into the basics of Dictionaries, and this came into my mind – ce0 Jan 02 '17 at 22:53
  • Dictionaries are mutable, so many of the methods on them *do* mutate. You don't assign e.g. the result of a `dict.update` (which returns `None`, per the convention in Python). Strings are immutable, so things are a bit different. – jonrsharpe Jan 02 '17 at 22:54
  • @ce0 in-place operations are even more confusing than reassigning the variable over and over again is tiresome. You don't know how many people are confused because `list.sort` works in-place and doesn't return anything. – MSeifert Jan 02 '17 at 22:55
  • Got what you guys are saying, thanks a lot by the way :) – ce0 Jan 02 '17 at 22:57

0 Answers0