Home  >  Article  >  Backend Development  >  Detailed introduction to Python variable naming

Detailed introduction to Python variable naming

高洛峰
高洛峰Original
2017-03-23 14:36:322684browse

The current development naming standards basically follow the camel case naming method, such as: userName. I will not characterize this specification anymore, everyone understands it. Next, let’s get to the point, how to choose a good name in python so that you can better understand your name?

1. Tuple type variable naming

Tuple type variables in python, such as:

schoolRoles = ("student","class monitor","teacher","schoolmaster")

We can correspondingly understand them as arrays in Java.

Recommendation: Tuple type variables should be named in the form of “variable name + s” or “variable name + Tuple”.

2. List type variable naming

List type variables in python: such as:

studentList = ["zhangsan", "lisi", "wangwu"]

This is similar to List in Java.

Recommendation: List type variables should be named in the form of “variable name + List”.

3. Dictionary type variable naming

Dict type variables in python: such as:

studentDict = {"name":"zhangsan", "age":"18", "sex":"男"}

This is similar to the json data format.

Suggestion: Dict type variables should be named in the form of "variable name + Dict".

4. Naming of Set type variables

The Set type variable in python is similar to the Set in Java, which is an unordered sequence of non-repeating elements.

Recommendation: Set type variables should be named in the form of "variable name + Set".

5. Apply Tuple or Dict to List or Set

For example:

    studentTupleList = [("zhangsan","lisi","wangwu"),("liubei","guanyu","zhangfei")],
    studentDictList = [{"name":"zhangsan", "age":"18", "sex":"男"},{"name":"lisi", "age":"20", "sex":"男"}]

Set just like List.

Suggestion: When applying Tuple or Dict to List or Set, name it in the form of "variable name + Tuple or Dict + List or Set".

Summary: Simple types end with the type name, and complex list or set types end with "type name + List or Set", so that we can better understand the variable names when using python The meaning contained and the information stored and how it is stored.

The above are some personal opinions and suggestions. If you have any better suggestions, please give us timely feedback so that we can learn from each other.

The above is the detailed content of Detailed introduction to Python variable naming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn