Python命名约定

2024-10-01 15:29:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用flaskrestful在python应用程序中实现restapi。我有一个模型模块,一个业务模块和一个控制器模块。这是我在登录中定义的一个控制器_控制器.py文件

from flask.ext.restful import Resource,request,reqparse
from app.business.login import Login
from app.models.models import User, Address

class Login_Controller(Resource):

    def __init__(self):
        pass
    def get(self):
        loginBO=Login()
        obj=loginBO.getAllUsers()
        return {"users":obj}, 201
    def post(self):
       pass
    def delete(self):
        loginBO=Login()
        status =loginBO.deleteUser(request.json)
        if status:
            return {"status":"true"},201
        else:
            return {"status":"false"},401
    def put(self):
        loginBO=Login()
        status =loginBO.addUser(request.json)
        if status:
            return {"status":"true"},201
        else:
            return {"status":"false"},401

现在我对将上述文件命名为控制器不是很满意吗?“Python”的方式是什么

  1. 命名文件
  2. 命名类。在

我见过一些人把这些类命名为视图。如果我路由到一个模板,这看起来不错,但是我提供的是普通的老json?你们说呢?在


Tags: 模块文件fromimportselfjsonreturnrequest
2条回答

Python标准命名约定如PEP 8所示。你问过文件名和类名。package and module names(通常是您的文件名)的约定:

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

Since module names are mapped to file names, and some file systems are case insensitive and truncate long names, it is important that module names be chosen to be fairly short this won't be a problem on Unix, but it may be a problem when the code is transported to older Mac or Windows versions, or DOS.

When an extension module written in C or C++ has an accompanying Python module that provides a higher level (e.g. more object oriented) interface, the C/C++ module has a leading underscore (e.g. _socket).

对于class names

Almost without exception, class names use the CapWords convention. Classes for internal use have a leading underscore in addition.

就这样。对于您是将其称为“controller”或“view”或“resource”没有约定,甚至名称中甚至有“controller”或“view”之类的名称。这里没有正式的“python约定”,特别是考虑到web应用程序只是python所能做的一小部分。只要项目中的其他开发人员理解“控制器”与“视图”的含义,就可以了。不要想得太多,也不要指望别人能给出一个正确的答案,这只是我们作为开发人员的负担:)

但是,如果您将其称为LoginController,请确保它是LoginController,而不是{}。在

你可以按照pep8(http://www.python.org/dev/peps/pep-0008/#package-and-module-names)来做,这是一种更为Python式的方式。你也可以用Google Style Guide,我想可以。在

在这个特定的例子中,我将为所有控制器使用一个名为controller的文件,我的类将命名为Login。但是,如果你真的喜欢登录名.py对于你的模块,我想最好用controller来命名你的控制器。在

相关问题 更多 >

    热门问题