Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

python - Python中变量和函数名称的命名约定是什么?(What is the naming convention in Python for variable and function names?)

Coming from a C# background the naming convention for variables and method names are usually either camelCase or PascalCase:

(来自C#背景,变量和方法名称的命名约定通常是camelCase或PascalCase:)

    // C# example
    string thisIsMyVariable = "a"
    public void ThisIsMyMethod()

In Python, I have seen the above but I have also seen underscores being used:

(在Python中,我已经看到了上面的内容,但我也看到了使用下划线:)

    # python example
    this_is_my_variable = 'a'
    def this_is_my_function():

Is there a more preferable, definitive coding style for Python?

(Python有更优选的,明确的编码风格吗?)

  ask by Ray Vega translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See Python PEP 8 .

(请参阅Python PEP 8 。)

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

(函数名称应为小写,并根据需要用下划线分隔,以提高可读性。)

mixedCase is allowed only in contexts where that's already the prevailing style

(只有在已经成为流行风格的情境中才允许使用mixedCase)

Variables...

(变量...)

Use the function naming rules: lowercase with words separated by underscores as necessary to improve readability.

(使用函数命名规则:小写,必要时用下划线分隔,以提高可读性。)

Personally, I deviate from this because I also prefer mixedCase over lower_case for my own projects.

(就个人而言,我与此不同,因为我也喜欢mixedCaselower_case为我自己的项目。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...