我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用ast.YieldFrom()。
def check_for_b901(self, node): xs = list(node.body) has_yield = False return_node = None while xs: x = xs.pop() if isinstance(x, (ast.AsyncFunctionDef, ast.FunctionDef)): continue elif isinstance(x, (ast.Yield, ast.YieldFrom)): has_yield = True elif isinstance(x, ast.Return) and x.value is not None: return_node = x if has_yield and return_node is not None: self.errors.append( B901(return_node.lineno, return_node.col_offset) ) break xs.extend(ast.iter_child_nodes(x))
def visit_For(self, node: ast.For): node = self.generic_visit(node) if self._is_for_yield(node): yield_node = ast.YieldFrom(value = node.iter) expr_node = ast.Expr(value = yield_node) node = ast.copy_location(expr_node, node) node = ast.fix_missing_locations(node) return node
def test_yield(self): self.expr(ast.Yield(ast.Name("x", ast.Store())), "must have Load") self.expr(ast.YieldFrom(ast.Name("x", ast.Store())), "must have Load")
def _isyield(el): return isinstance(el, (ast.Yield, ast.YieldFrom))
def is_a_generator(function): return has_nodes(function.tree, (ast.Yield, ast.YieldFrom))