For graceful failure: return None or False instead of exceptions, let caller decide how to handle failure.
Return None (or False) to indicate failure, let caller check.
def find(items, predicate):
"""Find first item matching predicate, or None."""
for item in items:
if predicate(item):
return item
return None # Not found
# Caller checks
result = find(items, is_valid)
if result is not None:
process(result)
else:
handle_not_found()
# Sudoku solver (sudoku.py)
def search(values):
"""Using depth-first search and propagation."""
if values is False:
return False # Failed earlier
if all(len(values[s]) == 1 for s in squares):
return values # Solved!
# Choose unfilled square with fewest possibilities
n, s = min((len(values[s]), s)
for s in squares if len(values[s]) > 1)
for d in values[s]:
result = search(assign(values.copy(), s, d))
if result: # Found solution
return result
return False # No solution found
# Constraint propagation
def eliminate(values, s, d):
"""Return values, or False if contradiction detected."""
if d not in values[s]:
return values # Already done
values[s] = values[s].replace(d, '')
if len(values[s]) == 0:
return False # Contradiction
# ... more propagation
return values
# Usage pattern
solution = solve(puzzle)
if solution:
display(solution)
else:
print("No solution exists")
# Cryptarithmetic (Cryptarithmetic.ipynb)
def first(iterable):
"""First element of iterable, or None."""
return next(iter(iterable), None)
solution = first(solve('SEND + MORE = MONEY'))
if solution:
print(solution)
if: Natural control flowif result is False: return Falseif result: catches None/Falsenpx skills add jimmc414/return-none-for-failure下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer