简体   繁体   English

python中同名的变量和常量

[英]Variable and constant with the same name in python

If I execute the following code in python on CPython interpreter, it works as expected:如果我在 CPython 解释器的 python 中执行以下代码,它将按预期工作:

A = 1
a = 2
print(A)
print(a)

Output: Output:

1
2

The question is will this behavior persist with other implementations of Python interpreters?问题是这种行为是否会在 Python 解释器的其他实现中持续存在? Can I rely on this and produce such a code?我可以依靠这个并生成这样的代码吗?

These effectively are just two different variables, so I would imagine that this behaviour will persist in all systems.这些实际上只是两个不同的变量,所以我想这种行为将在所有系统中持续存在。

By convention, constants are upper-case.按照惯例,常量是大写的。 However, Python is seeing two completely different variables, as it's a case-sensitive language.但是,Python 看到两个完全不同的变量,因为它是区分大小写的语言。

Not entirely sure if relevent, but see here for naming conventions.不完全确定是否相关,但请参阅此处了解命名约定。

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

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