简体   繁体   English

Python中不同类型的变量名称相同

[英]Same variable name for different types in Python

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. 这会将变量msg的类型从list更改为str ,这是像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? 这里的分配紧密并没有太多的混乱,但是如果在宽间隔的代码位中以两种不同的形式使用msg怎么办?

but what if msg were used in its two different guises in widely-spaced bits of code? 但是,如果在宽间隔的代码位中以两种不同的形式使用msg怎么办?

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)

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. 第一行带有一个值(特别是一个列表),并在其上加上标签,因为正如注释中提到的Sylvain Leroux所说的,这实际上是一个变量。 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. 另一方面,如果您实际上需要两个不同的值(一个基于另一个值),则在代码的其他地方独立使用,则一定要创建一个不同的变量名,如马丁·科内西尼(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'; 实际上,用几个不同的“标签”来“标记”一个值是很标准的。 ie 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. 这里重要的是值,而不是变量,变量只是名称空间中的标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM