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
cevincheung
V2EX  ›  Python

玩了一下午的 tornado,提几个问题

  •  
  •   cevincheung ·
    cevin · 2015-02-02 19:10:53 +08:00 · 2718 次点击
    这是一个创建于 3379 天前的主题,其中的信息可能已经有所发展或是发生改变。
    """
    自定义错误页面
    """
    class errorController(userController):
        def get(self):
            self.write('404 not found');
        def post(self):
            errorController.get(self);
    

    怎么实现类型这样的效果:

    """
    自定义错误页面
    """
    class errorController(userController):
        def get(self):
        def post(self):
        def delete(self):
        # options put ......
            self.write('404 not found/ method not allowed');
    

    然后是关于class继承

    class a():
        def __init__():
            pass;
    
    class b(a):
        def __init__():
            pass;
    

    像php有parant::__init()__可以执行继承的class的同名方法。python捏?
    还有就是像

    class baseController(tornado.web.RequestHandler):
    class userController(baseController):
    class panelController(userController):
    # 然后就莫名其妙500了……
    

    然后是怎么动态加载一些python文件,比如:

    a = 'a';
    import a;
    # 但是即便实现了好像也没用,python貌似所有执行过程中需要的文件都需要预先import……
    # 像php   if($get[a]) include_once $get[a].php
    

    暂时这么多……

    5 条回复    2015-02-03 10:11:07 +08:00
    dddd
        1
    dddd  
       2015-02-02 19:15:27 +08:00   ❤️ 1
    基础比我还差还是先放下 tornado 吧……
    14
        2
    14  
       2015-02-02 19:44:40 +08:00
    class b(a):
    ....def __init__(self):
    ........super(b).__init__(self)

    a = __import__('a')
    yetone
        3
    yetone  
       2015-02-02 20:42:27 +08:00
    zhwei
        4
    zhwei  
       2015-02-03 07:27:38 +08:00
    1、

    ```python

    class errorController(userController):
    def get(self):
    self.write('404 not found') # 没有分号

    post = delete = get

    ```

    2、楼上正解 `super()`
    3、[importlib](https://docs.python.org/3/library/importlib.html)


    PS. 这些跟Tornado没什么关系, 都是Python基础语法,初学Python并且想做web的话可以用些更轻量的框架。
    zenliver
        5
    zenliver  
       2015-02-03 10:11:07 +08:00
    你还是先把基础搞好吧,,,
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2471 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:18 · PVG 20:18 · LAX 05:18 · JFK 08:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.