1

Extremely basic question that I don't quite get.

If I have this line:

my_string = "how now brown cow"

and I change it like so

my_string.split()

Is this acceptable coding practice to just straight write it like that to change it? or should I instead change it like so:

my_string = my_string.split()

don't both effectively do the same thing?

when would I use one over the other?

how does this ultimately affect my code?

2 Answers 2

1

always try to avoid:

my_string = my_string.split()

never, ever do something like that. the main problem with that is it's going to introduce a lot of code bugs in the future, especially for another maintainer of the code. the main problem with this, is that the result of this the split() operation is not a string anymore: it's a list. Therefore, assigning a result of this type to a variable named my_string is bound to cause more problems in the end.

Sign up to request clarification or add additional context in comments.

4 Comments

I do this all the time. Sometimes I'll have three or four successive lines of transformation before I've got what I want. But I take your point, it can be dangerous to keep reusing the same variable to hold different types.
I guess in this case it's a little bit different. The result of the string operation is not a string, so you just want to ensure the variable is not incorrectly named (which will hopefully save you from a lot of debugging later)
Thanks. Im still trying to understand what the right way to write this is? I also tend to do this a lot and didn't know it was bad practice. Say one is in the main method, it is better to write it using just my_string.split()? I also came to similar question which brings up in-place: here [link]stackoverflow.com/a/5846010/17082363[link]
Ah, now i understand the context for that mentioned approach. Actually, the one in the linked question is very different in this case. The reverse method actually modifies the list in place. In Python, strings are immutable types, so the methods always need to be assigned to a variable (or reassigned to the same one). So for example, string.strip() doesn’t modify the string in place, it needs to be reassigned to the string variable to have the desired result.
0

The first line doesn't actually change it - it calls the .split() method on the string, but since you're not doing anything with what that function call returns, the results are just discarded.

In the second case, you assign the returned values to my_string - that means your original string is discarded, but my_string no refers to the parts returned by .split().

Both calls to .split() do the same thing, but the lines of your program do something different.

You would only use the first example if you wanted to know if a split would cause an error, for example:

try:
    my_string.split()
except:
    print('That was unexpected...')

The second example is the typical use, although you could us the result directly in some other way, for example passing it to a function:

print(my_string.split())

It's not a bad question though - you'll find that some libraries favour methods that change the contents of the object they are called on, while other libraries favour returning the processed result without touching the original. They are different programming paradigms and programmers can be very divided on the subject.

In most cases, Python itself (and its built-in functions and standard libraries) favours the more functional approach and will return the result of the operation, without changing the original, but there are exceptions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.