推荐学习书目
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 · 7404 views
    This topic created in 4180 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
        1
    ligyxy  
       Dec 30, 2014 via Android
    getattr()或者locals()
    hahastudio
        2
    hahastudio  
       Dec 30, 2014
    你需要一个 super class Foo,里面有 talk(self),fly(self)
    然后 class AAA(Foo), class BBB(Foo) 继承,重写 talk 和 fly 的实现
    handle_check 里传类的名字,比如 handle_check(AAA)
    函数里实例一个 AAA,然后再 talk,fly
    lookhi
        3
    lookhi  
       Dec 30, 2014 via Android
    直接点. {name:func}
    chevalier
        4
    chevalier  
       Dec 30, 2014
    看一下 eval
    yakczh
        5
    yakczh  
       Dec 30, 2014
    这是工场模式吗?
    iT2afL0rd
        6
    iT2afL0rd  
       Dec 30, 2014
    建议了解一下多态的特性!
    recall704
        7
    recall704  
    OP
       Dec 30, 2014
    @hahastudio 比较想用这种方法,但是不会写。
    recall704
        8
    recall704  
    OP
       Dec 30, 2014
    @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
        9
    recall704  
    OP
       Dec 30, 2014
    @yakczh 我就是想用工厂模式,但参数不对劲

    def talk(name):
    pass

    def fly(id):
    pass

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


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

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