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

函数依赖的功能可扩展,怎么写最好?

  •  
  •   smdbh · 2018-05-07 01:11:35 +08:00 · 2053 次点击
    这是一个创建于 2174 天前的主题,其中的信息可能已经有所发展或是发生改变。
    想实现的目标是,主函数 f_a(), 是个解析帧的函数,当前把不同帧格式的解析函数都写死在了主函数里。当然也可以做成 list 当参数传入。但使用这个函数就很奇怪,每次都要传这个 list,感觉这个接口不好。
    想问下有什么好的代码结构,可以解决这个问题。最好是能库函数和用户函数分离的结构。
    fushall
        1
    fushall  
       2018-05-07 09:59:52 +08:00
    方式很多:
    ```
    class X:
    def __init__(参数):
    pass

    def f_a(x):
    if isinstance(x, X):
    pass
    else:
    raise XXXXError('参数类型不对')
    f_a(X(xx,yy,zzz))

    def f_a(*args):
    pass

    f_a(*a_list)


    def f_a(**kwargs):
    pass

    f_a(x=xx,y=yy)

    ```
    enenaaa
        2
    enenaaa  
       2018-05-07 13:42:15 +08:00
    说实话没看懂你的需求是啥
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2996 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:10 · PVG 19:10 · LAX 04:10 · JFK 07:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.