1

I find myself doing the equivalent of something like this a lot:

msg = ['This', 'is', 'a', 'message']  # (or built up programmatically)
msg = ' '.join(msg)

which changes the type of the variable msg from a list to a str, as is allowed for a dynamically-typed language like Python. Is it a good idea, though? There's not much confusion here where the assignments are close together, but what if msg were used in its two different guises in widely-spaced bits of code?

6
  • isinstance(msg,list) ? I don't see a problem here. It can be hard to read sometimes but I find myself doing just this quite often. Just make sure you are counting with the right type Commented Aug 25, 2014 at 15:20
  • 2
    The real question is: What is the benefit of using the same label for a list and a str? Commented Aug 25, 2014 at 15:20
  • 1
    It can be very useful to you to give your variables descriptive names, even if it costs you extra key-strokes. Commented Aug 25, 2014 at 15:21
  • '.'.join('This is a message') == 'T.h.i.s. .i.s. .a. .m.e.s.s.a.g.e' Commented Aug 25, 2014 at 15:21
  • "which changes the type of the variable" No. It doesn't. Variable don't have a type in Python. They are not much more than tags referring objects in memory. Commented Aug 25, 2014 at 15:38

2 Answers 2

5

but what if msg were used in its two different guises in widely-spaced bits of code?

You've hit the nail on the head. Using the same reference name just adds one more thing for the programmer to keep note of when scanning the code - for this reason alone it's better to use a different name.

Personally I use something like:

msg = ['This', 'is', 'a', 'message']
msg_str = ' '.join(msg)
Sign up to request clarification or add additional context in comments.

3 Comments

Namespaces are one honking great idea
I understand the reasons for doing this, but I guess the reason I don't is that it feels unPythonic to attach the type names to my variable identifiers. I mean, why have dynamic typing at all if the variable names are going to indicate (but not enforce) the data type?
@user293594 you would generally avoid using types in the name. But in the situations where you would have a name clash, it is preferable (IMO) to append the type.
3

My suggestion is that you don in your code only what you actually intend to do, and nothing else. If you think about your example, what does it actually do?

The first line takes a value (specifically a list) and puts a label on it, because as Sylvain Leroux mentioned in the comment this is all a variable actually is. Then, in the second line you use that value to create another value (a string this time) and then -- this is the important part -- you take the label off of the first value and put it on the second.

Is this really what you wanted to do? Basically, the first value (the list) was a throwaway intermediate step to the final value that you really needed, and was discarded as its job was done. In this particular case, the code could have easily be written as:

msg = ' '.join(['This', 'is', 'a', 'message'])

On the other hand, if you actually need two different values, one based on the other, used independently elsewhere in the code, then by all means do create a different variable name, as suggested by martin Konecny.

The important thing here is to keep in mind that variables are actually labels applied to different values, and can be moved away from one value and assigned to another. In fact, it's quite standard to have a value 'labelled' with several different 'labels'; i.e. to assign a same value to multiple variables. The important bit here is the value, and not the variable, which is just a label in a namespace.

2 Comments

Thanks for your answer. I understand how variables, values and types work in Python, so my question was really one concerning style -- I can't build my msg str in the way you suggest because its pieces (items in the list) might be generated programmatically. But the msg list is just temporary -- I really do intend to throw it away when I build the msg str.
That's why I said "in this particular case", i.e. as written. But the relevant point is whether the value is a keeper or a throwaway.

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.