推荐学习书目
› 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
recall704
V2EX  ›  Python

python 怎么根据参数执行不同的函数?

  •  
  •   recall704 · Dec 30, 2014 · 7707 views
    This topic created in 4325 days ago, the information mentioned may be changed or developed.

    假如我有这样的方法

    def AAA_talk():
        pass
    
    def AAA_fly():
        pass
    
    def BBB_talk():
        pass
    
    def BBB_fly():
        pass
    

    然后我有一个函数

    def handle_check(name):
        pass
    

    h = handle_check(name="AAA")
    我就可以使用h.AAA_talk 和 h.AAA_fly
    如果 h = handle_check(name="BBB")
    我就可以使用h.BBB_talk 和 h.BBB_fly

    能构造个类什么的也可以,求方法思路?

    14 replies  •  2014-12-30 15:07:54 +08:00
    ligyxy
    ligyxy
    Dec 30, 2014 via Android
    1
    getattr()或者locals()
    hahastudio
    hahastudio
    Dec 30, 2014
    2
    你需要一个 super class Foo,里面有 talk(self),fly(self)
    然后 class AAA(Foo), class BBB(Foo) 继承,重写 talk 和 fly 的实现
    handle_check 里传类的名字,比如 handle_check(AAA)
    函数里实例一个 AAA,然后再 talk,fly
    lookhi
    lookhi
    Dec 30, 2014 via Android
    3
    直接点. {name:func}
    chevalier
    chevalier
    Dec 30, 2014
    4
    看一下 eval
    yakczh
    yakczh
    Dec 30, 2014
    5
    这是工场模式吗?
    iT2afL0rd
    iT2afL0rd
    Dec 30, 2014
    6
    建议了解一下多态的特性!
    recall704
    recall704
    OP
    Dec 30, 2014
    7
    @hahastudio 比较想用这种方法,但是不会写。
    recall704
    recall704
    OP
    Dec 30, 2014
    8
    @hahastudio 其实我真正想的是这样的,用一个工厂模式:

    class A(object):

    def talk(self):
    print 'A talk'
    def fly(self):
    print 'A fly'

    class B(object):

    def talk(self):
    print 'B talk'

    def fly(self):
    print 'B fly'

    class Factory(object):
    def __init__(self,name):
    if name == 'A':
    return A()
    elif name == 'B':
    return B()
    else:
    return None

    遇到的问题是,talk和fly 需要的参数不一样,有时候这个参数有,有时候没有。
    recall704
    recall704
    OP
    Dec 30, 2014
    9
    @yakczh 我就是想用工厂模式,但参数不对劲

    def talk(name):
    pass

    def fly(id):
    pass

    在实例化的时候,name和id有只存在一个,init的部分不好实例化。
    hahastudio
    hahastudio
    Dec 30, 2014
    10
    @recall704 唉,去学一下面向对象和设计模式吧
    例子:
    https://gist.github.com/hahastudio/4c544040a8026e408600


    扩展阅读,请搜索
    python design patterns
    14
    14
    Dec 30, 2014 via Android
    12
    hasattr和getattr
    1989922yan
    1989922yan
    Dec 30, 2014
    13
    1. 应该是 鸭子判别 的概念。
    你可以建立 class AAA 和 class BBB,在方法中,直接调用他们的方法(AAA 和 BBB的方法同名,不要变)

    2. 元编程,如果想玩,也可以用
    clino
    clino
    Dec 30, 2014
    14
    我之前用的是 locals() 来实现这种功能
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1250 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 21:13 · PVG 05:13 · LAX 13:13 · JFK 16:13
    ♥ Do have faith in what you're doing.