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
agentwx
V2EX    Python

python 动态生成 class 的问题

  •  
  •   agentwx 2015-11-21 18:33:20 +08:00 3622 次点击
    这是一个创建于 3663 天前的主题,其中的信息可能已经有所发展或是发生改变。

    昨天晚上看到一个 python 包 tornadows , 可以方便的用来创建 webservice
    如下
    from tornadows import soaphandler
    from tornadows import xmltypes
    from tornadows.soaphandler import webservice

    class MatchService(soaphandler.SoapHandler):
    @webservice(_params=[float,float],_returns=float)
    def add(self, a, b):
    result = a+b
    return result

    看了写这些类的形式差不多多少,但是又必须要写代码
    那么可不可以动态的生成这个类来被使用呢? 比如我给出一个配置信息,包括了类名,方法,方法的参数数量及类型,甚至是返回值
    如何根据这个配置信息把这个类动态创建出来呢?
    需要了解什么知识点?

    谢谢

    12 条回复    2015-11-23 11:30:01 +08:00
    pupboss
        1
    pupboss  
       2015-11-21 18:50:44 +08:00
    可以看看 python 的元编程,之前有过接触,不过我也没搞明白
    ethego
        2
    ethego  
       2015-11-21 19:04:59 +08:00
    在廖雪峰的 python 教程里搜索"metaclass"
    justfly
        3
    justfly  
       2015-11-21 19:48:03 +08:00
    使用 type
    DEMONUNTER
        4
    DEMONHUNTER  
       2015-11-21 20:33:12 +08:00
    当然可以, python 的 mataclass 就是专门干这个的。
    lxy42
        5
    lxy42  
       2015-11-21 20:37:55 +08:00
    type +1
    baichi
        6
    baichi  
       2015-11-21 21:31:34 +08:00
    echo1937
        7
    echo1937  
       2015-11-21 21:42:17 +08:00
    我大概看过 50 页元编程,发现这玩意十足黑科技。
    ericls
        8
    ericls  
       2015-11-22 09:22:56 +08:00
    meta programming in python

    我用这个实现过一个小东西

    http://leeeric.com/post/a-python-web-crawler-helper/
    agentwx
        9
    agentwx  
    OP
       2015-11-22 22:51:41 +08:00
    metaclass 怎么玩呢?感觉没有这个东西,好像也是可以动态的嘛。比如
    In [1]: def func_a(cls, a, b):
    ...: return a+b
    ...:

    In [2]: class A(object):
    ...: pass
    ...:

    In [3]: A.proper1 = "a"

    In [4]: A.funca = func_a

    In [5]: t = A()

    In [6]: t.funca(1,2)
    Out[6]: 3


    正确的玩法,那么 metaclass 的正确的玩法是怎么样的呢?
    agentwx
        10
    agentwx  
    OP
       2015-11-22 22:54:04 +08:00
    @ericls 多谢,我研究下
    neoblackcap
        11
    neoblackcap  
       2015-11-23 10:47:25 +08:00
    metaclass ,我不了解。我只是了解 type 函数用法而已,请去看看 type 函数的文档,自然可以解决你的问题
    agentwx
        12
    agentwx  
    OP
       2015-11-23 11:30:01 +08:00
    @neoblackcap 好的 多谢
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1271 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 17:11 PVG 01:11 LAX 09:11 JFK 12:11
    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