Which abbreviations should we use for python variable names?

后端 未结 5 1142
说谎
说谎 2021-02-08 19:27

In generally I\'m using the standard naming stated in PEP-8 for variables. Like:

delete_projects
connect_server

However sometimes I can\'t find

5条回答
  •  无人共我
    2021-02-08 19:37

    In your example, you have a function called delete_project. Wondering what to call the variable that stores the project to be deleted? Just 'project'!

    def delete_project(self, project):
        del self.projects[project]
    

    Simple.

    Variable names don't have to be fully descriptive. Context can lend a lot to how we understand a particular name at a particular point in time. No need to say "this is the project to be deleted" when discussing a function that deletes project.

    If you find function names are too long, they're probably doing too much. If you find variable names are becoming too long, think about their purpose in the current context, and see if part of the name can be implied.

提交回复
热议问题