python函数的嵌套调用,在Python中调用嵌套函数

I have a method that i have broken into smaller nested functions to break up the code base:

def foo(x,y):

def do_this(x,y):

pass

def do_that(x,y):

pass

do_this(x,y)

do_that(x,y)

return

Is there a way to run one of the nested functions by itself. eg:

foo.do_this(x,y)

EDIT:

I am trying to setup caching on a web server i have built using pyramid_breaker

def getThis(request):

def invalidate_data(getData,'long_term',search_term):

region_invalidate(getData,'long_term',search_term)

@cached_region('long_term')

def getData(search_term):

return response

search_term = request.matchdict['searchterm']

return getData(search_term)

This is my understanding may not be accurate:

Now the reason i have this is that the namespace used by the decorator to create the cache key is genereated from the function and the arguements. You can't therefore just put the decorator on getThis as the request variable is unique-ish and the cache is useless. So i created the inner function which has repeatable args (search_term).

However to invalidate the cache (ie refresh), the invalidation function requires scope to know of the 'getData' function so also needs to be nested. Therefore i need to call the nested function. You wonderful people have made it clear its not possible so is someone able to explain how i might do it with a different structure?

解决方案

I assume do_this and do_that are actually dependent on some argument of foo, since otherwise you could just move them out of foo and call them directly.

I suggest reworking the whole thing as a class. Something like this:

class Foo(object):

def __init__(self, x, y):

self.x = x

self.y = y

def do_this(self):

pass

def do_that(self):

pass

def __call__(self):

self.do_this()

self.do_that()

foo = Foo(x, y)

foo()

foo.do_this()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值