Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #3784
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail |
|---|---|
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
| Newsgroups | comp.lang.python |
| Subject | Re: Feature suggestion -- return if true |
| Date | Thu, 21 Apr 2011 12:13:43 +0200 |
| Organization | A newly installed InterNetNews server |
| Message-ID | <iop00o$b57$1@r03.glglgl.eu> (permalink) |
| References | <8abff237-5ccd-4eb6-85c8-cdc9e87520b7@bl1g2000vbb.googlegroups.com> <mailman.233.1302568099.9059.python-list@python.org> <87k4eze4dl.fsf@mithlond.arda> <BANLkTikrmLzySDVUNZhAWZR-JSB58r6tdA@mail.gmail.com> <BANLkTinz=JhS6z5orBPj2ZK+2z3kXFfa6Q@mail.gmail.com> <mailman.291.1302648982.9059.python-list@python.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| User-Agent | Mozilla/5.0 (X11; U; Linux i686; de; rv:1.9.2.14) Gecko/20110221 SUSE/3.1.8 Thunderbird/3.1.8 |
| In-Reply-To | <mailman.291.1302648982.9059.python-list@python.org> |
| Lines | 56 |
| NNTP-Posting-Date | 21 Apr 2011 12:20:02 CEST |
| NNTP-Posting-Host | 1d621adb.newsspool2.arcor-online.net |
| X-Trace | DXC=;5PLO]PWR<\5TOT9_N5i<VA9EHlD;3YcR4Fo<]lROoRQ8kF<OcfhCO[JIPCdN8i_QPK8FCa6^2FWROLF_]FfFg8_Yg3VEfCX>H^ |
| X-Complaints-To | usenet-abuse@arcor.de |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:3784 |
Show key headers only | View raw
Am 13.04.2011 01:06, schrieb Ethan Furman:
> --> def func():
> --> var1 = something()
> --> var2 = something_else('this')
> --> return? var1.hobgle(var2)
> --> var3 = last_resort(var1)
> --> return var3.wiglat(var2)
This makes me think of a decorator which can mimic the wantend behaviour:
def getfirst(f):
from functools import wraps
@wraps(f)
def func(*a, **k):
for i in f(*a, **k):
if i: return i
return func
# [BTW: a kind of "return decorator" would be nice here ;-)]
@getfirst
def func():
var1 = something()
var2 = something_else('this')
yield var1.hobgle(var2)
var3 = last_resort(var1)
yield var3.wiglat(var2)
yield "Even that did not work."
This has the advantage of being flexible about which condition to
evaluate: maybe the func does return tuples of which only the 2nd part
is relevant concerning the check. Then just do
def getfirst(f):
from functools import wraps
@wraps(f)
def func(*a, **k):
for i in f(*a, **k):
if i[1]: return i
return func
@getfirst
def func():
var1 = something()
var2 = something_else('this')
yield "first try", var1.hobgle(var2)
var3 = last_resort(var1)
yield "second try", var3.wiglat(var2)
yield "default value", "Even that did not work."
Disclaimer: Untested, but you should get the idea.
Thomas
Back to comp.lang.python | Previous | Next | Find similar
Re: Feature suggestion -- return if true Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-04-21 12:13 +0200
csiph-web