V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
supersheep
V2EX  ›  Python

python可不可以单文件单class?

  •  
  •   supersheep · Sep 19, 2013 · 5248 views
    This topic created in 4607 days ago, the information mentioned may be changed or developed.
    就是我在用web.py写网站嘛
    然后一开始写了个router

    prefix = "controller.page."

    urls = (
    '/a', prefix + 'a',
    '/b', prefix + 'b'
    )

    然后在 /controller/page.py 中写

    class a:
    GET(self):
    ...

    class b:
    GET(self):
    ...

    随着页面越来越多,我希望把每个页面都放到单独的文件中去,于是就有了

    /controller
    /page
    __init__.py
    a.py
    b.py

    但是我发现这样就不能通过 controller.page.a 引用到 class a 了,要写成 controller.page.a.a 才行

    现在就想知道python能不能做到类似nodejs中 module.exports = function(){} 这样的写法?

    如果可以就最好,不行的话就只能写个约定好的命名规范去对应类名或者重新考虑页面与模块的归类了
    12 replies    1970-01-01 08:00:00 +08:00
    9hills
        1
    9hills  
       Sep 19, 2013 via Android
    这种情况一般是在init.py里from .a import a

    或者干脆 from .a import *
    supersheep
        2
    supersheep  
    OP
       Sep 19, 2013
    @9hills soga,多谢。好像from .a这里的"."不是必须的。不过这样以后每加一个页面就还要再在__init__ 里面加一下,感觉不够傻瓜,现在是写了个方法,一开始的时候parse自己写的一个dict变成tuple传给webpy这样子
    aisk
        3
    aisk  
       Sep 19, 2013
    何必一个类一个文件,楼主用的web.py,一个类应该就是一个页面,难道每个页面的处理代码都非常长,必须拆成一个文件?
    supersheep
        4
    supersheep  
    OP
       Sep 19, 2013
    @aisk 也还好,个别页面超过50行了。不过分到页面的话根据模板找文件改起来定位会更快一些,个人喜好啦。
    yakczh
        5
    yakczh  
       Sep 19, 2013
    分开文件,多人合作的时候方便些
    9hills
        6
    9hills  
       Sep 20, 2013 via Android
    @supersheep 用import *啊 另外.是有深意的
    supersheep
        7
    supersheep  
    OP
       Sep 20, 2013
    @9hills 表示在当前目录么?我试了一下,from . import * 不行,from . import a,b 倒是可以的。
    9hills
        8
    9hills  
       Sep 20, 2013
    9hills
        9
    9hills  
       Sep 20, 2013
    @supersheep
    Imports can be ambiguous in the face of packages; within a package, it's not clear whether import foo refers to a module within the package or some module outside the package. (More precisely, a local module or package can shadow another hanging directly off sys.path.)

    For the second problem, it is proposed that all import statements be absolute by default (searching sys.path only) with special syntax (leading dots) for accessing package-relative imports.

    意思是说如果你用import aaa,就无法分辨是import本目录的aaa还是sys.path中的aaa

    所以如果是确定要import package内部,就要加点
    supersheep
        10
    supersheep  
    OP
       Sep 20, 2013
    @9hills 好的!非常感谢!我好好看看!
    tkdchen
        11
    tkdchen  
       Sep 23, 2013   ❤️ 1
    @supersheep Add `from a import *` into file __init__.py under directory controller/page

    Or give the full reference path based on the path of your project, `from controller.page.a import *`

    Better not use dot in from-import statement

    Sorry for inputting English, cannot use Chinese while writing this.
    tonic
        12
    tonic  
       Sep 23, 2013
    在__init__里, from controller.page.a import a = =
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2406 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 10:41 · PVG 18:41 · LAX 03:41 · JFK 06:41
    ♥ Do have faith in what you're doing.