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

python传参数能传一个函数进去吗?

  •  
  •   yakczh · 2013-08-08 15:47:35 +08:00 · 3364 次点击
    这是一个创建于 3922 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如 
    def cache(store,datasource):


    这个datasorce可能是一个url,也可能是从读数据库, 在调用的时候传一个函数读取数据源, 业务代码统一用 cache(store,datsource)这样的方式调用,不用区别是数据库,还是网络
    14 条回复    1970-01-01 08:00:00 +08:00
    xingxiucun
        1
    xingxiucun  
       2013-08-08 15:49:24 +08:00
    可以
    yakczh
        2
    yakczh  
    OP
       2013-08-08 15:56:52 +08:00
    @xingxiucun 能写个简单的例子吗
    xingxiucun
        3
    xingxiucun  
       2013-08-08 16:02:25 +08:00
    @yakczh
    1 def a():
    2 print 'a'
    3
    4 def b():
    5 print 'b'
    6
    7
    8
    9 def e(m):
    10 m()
    11
    12
    13
    14 e(a)

    不清楚你是不是这个意思
    TK
        4
    TK  
       2013-08-08 16:07:27 +08:00
    >>> def eval(fun, a):
    ... print(fun(a))
    ...
    >>> eval(sum, [1, 2])
    3
    binux
        5
    binux  
       2013-08-08 16:10:52 +08:00
    如果类型不同的时候需要在cache里自行判断参数类型,以做不同动作
    larkifly
        6
    larkifly  
       2013-08-08 16:13:34 +08:00
    这个必须可以呀
    yakczh
        7
    yakczh  
    OP
       2013-08-08 16:27:56 +08:00
    @xingxiucun  如果a要带参数的话,怎么写呢
    qdvictory
        8
    qdvictory  
       2013-08-08 16:31:15 +08:00
    @yakczh m(a,'hello word')
    def m(func,str):
    funcA(str)

    def funcA(str):
    print str
    qdvictory
        9
    qdvictory  
       2013-08-08 16:32:14 +08:00
    @yakczh m(a,'hello word')
    def m(func,str):
    func(str)

    def a(str):
    print str

    上一条搞混乱了- -
    likuku
        10
    likuku  
       2013-08-08 16:35:34 +08:00
    @yakczh

    >>> def a (k):
    ... print k
    ...
    >>> a('s')
    s
    >>> def b(e):
    ... e
    ...
    >>> b(a('lll'))
    lll
    sivacohan
        11
    sivacohan  
       2013-08-08 23:28:11 +08:00
    你要看的是装饰器
    TK
        12
    TK  
       2013-08-09 00:54:32 +08:00 via Android
    @yakczh 看我的上一条回复。。。。。
    yakczh
        13
    yakczh  
    OP
       2013-08-09 11:49:31 +08:00
    如果 要传入的函数带参数,其中一部分参数是主流程的数据,一部分参数是调用时动态传入的,这种情况怎么下

    比如

    def linkhandle(urls,prefix):

    urls=list(map( lambda x: x.replace('./',prefix), urls ))
    print(urls)
    return urls




    def workflow(handle):

    urls=['./201207/t20120705_1887040.html', './201206/t20120608_1846662.html']
    link=handle(urls)


    urls=workflow(linkhandle("http://"))
    urls=workflow(linkhandle('https://'))

    print(urls)

    将主流程中的url链接通过传入handle+参数 来重新格式化
    bh3887
        14
    bh3887  
       2013-08-09 12:03:07 +08:00
    可以参阅decorator
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1337 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 17:08 · PVG 01:08 · LAX 10:08 · JFK 13:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.