python中多条件语句_Python中的带条件语句

所以我编写了这段代码;它的调用方式如下:

with c_with(needs_with(), lambda: get_stuff()) as gs:

##DOESN't call get_stuff() unless needs_with is called.

# do nearly the same large block of stuff,

# involving gs or not, depending on needs_with()

特性:

除非条件为真,否则它不会调用with c_with(needs_with(), lambda: get_stuff(), lambda: dont_get_stuff()) as gs:

如果条件为假,则提供一个虚拟的上下文管理器。 (可能用python> = 3.7替换为with c_with(needs_with(), lambda: get_stuff(), lambda: dont_get_stuff()) as gs:)

(可选)您可以在条件为假的情况下发送替代的contextmanager:

with c_with(needs_with(), lambda: get_stuff(), lambda: dont_get_stuff()) as gs:

希望这会帮助某人!

-这是代码:

def call_if_lambda(f):

"""

Calls f if f is a lambda function.

From https://stackoverflow.com/a/3655857/997253

"""

LMBD = lambda:0

islambda=isinstance(f, type(LMBD)) and f.__name__ == LMBD.__name__

return f() if islambda else f

import types

class _DummyClass(object):

"""

A class that doesn't do anything when methods are called, items are set and get etc.

I suspect this does not cover _all_ cases, but many.

"""

def _returnself(self, *args, **kwargs):

return self

__getattr__=__enter__=__exit__=__call__=__getitem__=_returnself

def __str__(self):

return ""

__repr__=__str__

def __setitem__(*args,**kwargs):

pass

def __setattr__(*args,**kwargs):

pass

class c_with(object):

"""

Wrap another context manager and enter it only if condition is true.

Parameters

----------

condition: bool

Condition to enter contextmanager or possibly else_contextmanager

contextmanager: contextmanager, lambda or None

Contextmanager for entering if condition is true. A lambda function

can be given, which will not be called unless entering the contextmanager.

else_contextmanager: contextmanager, lambda or None

Contextmanager for entering if condition is true. A lambda function

can be given, which will not be called unless entering the contextmanager.

If None is given, then a dummy contextmanager is returned.

"""

def __init__(self, condition, contextmanager, else_contextmanager=None):

self.condition = condition

self.contextmanager = contextmanager

self.else_contextmanager = _DummyClass() if else_contextmanager is None else else_contextmanager

def __enter__(self):

if self.condition:

self.contextmanager=call_if_lambda(self.contextmanager)

return self.contextmanager.__enter__()

elif self.else_contextmanager is not None:

self.else_contextmanager=call_if_lambda(self.else_contextmanager)

return self.else_contextmanager.__enter__()

def __exit__(self, *args):

if self.condition:

return self.contextmanager.__exit__(*args)

elif self.else_contextmanager is not None:

self.else_contextmanager.__exit__(*args)

#### EXAMPLE BELOW ####

from contextlib import contextmanager

def needs_with():

return False

@contextmanager

def get_stuff():

yield {"hello":"world"}

with c_with(needs_with(), lambda: get_stuff()) as gs:

## DOESN't call get_stuff() unless needs_with() returns True.

# do nearly the same large block of stuff,

# involving gs or not, depending on needs_with()

print("Hello",gs['hello'])

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值