Python 变量命名规则——你需要知道的10点

https://medium.com/@dasagrivamanu/python-naming-conventions-the-10-points-you-should-know-149a9aa9f8c7

Coding standards are a set of guidelines to produce maintainable and scalable code. And how good are you at following them could decide the longevity of your career in Software Development. Since I code in Python, so I’ll be talking about the Python naming conventions in this post.

I’ve derived this information from Python’s PEP8 documentation and tried to put it in simple word. So I could make it easier for the readers to digest.

Also, do remember that naming convention is just one of the facets out of many Python coding standards. You can read about the other factors from the PEP8 documentation.

With this information, you can ensure code quality, readability, and re-usability. However, you can refer this Python code optimization guide to improve the performance and robustness of your Python applications.

Naming Conventions in Python

I. What to keep and what to avoid?

  • How to start?
    Don’t use names that look too general or wordy.
    Maintain a fine line between the two.
  • What is bad?
    user_list, moveInts, swapNums, dict_to_store_defns_of_a_word
  • What is good?
    user_info, move_integers, swap_numbers, word_definitions.
  • Things to note-
    Don’t be naive and name things like “X,” “Y” or “Z.”
    Use CamelCase only when it makes sense.

II. How to name a Package in python?

  • Only use lower case while naming a Package in Python.
  • Use an underscore as a separator to join multiple words when needed and produce a logical name.
  • However, keeping a short name (preferably one word long) is what PEP 8 prefer.

III. What should be my Module name?

  • Python loves the small case alphabets and hence suggests Module names should also follow the suite.
  • Like Packages, combine multiple words with an underscore to generate a meaningful module name.
  • Don’t think twice if you already have a perfect single word name for your module.

IV. How to choose a Classe name in Python?

  • Classes in Python subject to a different naming scheme called as CapWords convention.
  • In some cases, if the class is callable (allows you to use round parenthesis) and has proper documentation, then you can name them like functions.
  • Contrary to the above principle, Python has its built-in classes with names in lowercase.
  • Exception classes should always have trailing word as Error.

V. Should I name Globals differently?

  • Global variables must all be in lowercase.
  • The underscore should be used as a separator while forming a variable with multiple words.

VI. What is a good Instance variable name?

  • Instance variable names should follow the lowercase convention.
  • Have the underscore as a separator while naming a multi-word instance variable.
  • Begin a non-public instance variable name with a single underscore.
    Use two consecutive underscores at the beginning of an instance variable. It will get the name mangled (kind of like a private member).

VII. Don’t mess with a Method name in Python.

  • Make only lowercase Method names.
  • An underscore should separate words in a method name.
  • The non-public methods should have a leading underscore.
  • Mangling a method name will require adding two underscores at the beginning.

VIII. Never ignore the Arguments of a method.

  • Every instance method should have ‘self’ as its first argument.
  • All class methods should have ‘cls’ as their first argument.

IX. A function name should convey what it does.

  • Function names should follow the lowercase naming convention.
  • They should also use an underscore to separate words in a function name.

X. Ignorance is bliss but not in case of Constants in Python.

  • You should be able to identify a Constant just by looking at its name.
  • Hence, they all must be in a fully capitalized format.
  • Words should separate by an underscore to form a long constant name.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值