python可不可以单文件单class? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 2013-09-19 13:25:57 +08:00 4899 次点击
    这是一个创建于 4409 天前的主题,其中的信息可能已经有所发展或是发生改变。
    就是我在用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 条回复    1970-01-01 08:00:00 +08:00
    9hills
        1
    9hills  
       2013-09-19 13:39:23 +08:00 via Android
    这种情况一般是在init.py里from .a import a

    或者干脆 from .a import *
    supersheep
        2
    supersheep  
    OP
       2013-09-19 14:17:32 +08:00
    @9hills soga,多谢。好像from .a这里的"."不是必须的。不过这样以后每加一个页面就还要再在__init__ 里面加一下,感觉不够傻瓜,现在是写了个方法,一开始的时候parse自己写的一个dict变成tuple传给webpy这样子
    aisk
        3
    aisk  
       2013-09-19 14:39:57 +08:00
    何必一个类一个文件,楼主用的web.py,一个类应该就是一个页面,难道每个页面的处理代码都非常长,必须拆成一个文件?
    supersheep
        4
    supersheep  
    OP
       2013-09-19 14:48:01 +08:00
    @aisk 也还好,个别页面超过50行了。不过分到页面的话根据模板找文件改起来定位会更快一些,个人喜好啦。
    yakczh
        5
    yakczh  
       2013-09-19 14:54:32 +08:00
    分开文件,多人合作的时候方便些
    9hills
        6
    9hills  
       2013-09-20 10:12:21 +08:00 via Android
    @supersheep 用import *啊 另外.是有深意的
    supersheep
        7
    supersheep  
    OP
       2013-09-20 16:50:53 +08:00
    @9hills 表示在当前目录么?我试了一下,from . import * 不行,from . import a,b 倒是可以的。
    9hills
        8
    9hills  
       2013-09-20 16:55:13 +08:00
    9hills
        9
    9hills  
       2013-09-20 16:58:51 +08:00
    @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
       2013-09-20 17:05:25 +08:00
    @9hills 好的!非常感谢!我好好看看!
    tkdchen
        11
    tkdchen  
       2013-09-23 13:19:17 +08:00   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  
       2013-09-23 16:04:24 +08:00
    在__init__里, from controller.page.a import a = =
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     989 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 28ms UTC 18:41 PVG 02:41 LAX 11:41 JFK 14:41
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86