发现 Python 一个有意思的小特性,发现很合适搞成面试题。问了 AI 都不行:),欢迎来挑战~

查看 245|回复 26
作者:xFrank   
已知如下 python 代码文件(已经是全部内容):
def foo():
    print("start")
    xxxxxx
    print("end")
if __name__ == "__main__":   
    print("1111")  
    foo()
    print("2222")
问题:请将 xxxxx 替换为一行 python 代码,使这个 python 文件执行后最终输出只有 1111 和 2222
替换代码要求:只能有一个语句,只有一行;多个语句组合的不行( import 也算一个语句,因此类似 import sys; sys.xxx 的不行)
引申问题:上述替换代码要求放宽为“允许引入自带库并允许多条语句”,是否还有别的方案?

Python, 面试题, 特性

uorz   
Gemini: yield.
maolon   


你确定你问对了 ai ?
uorz   
I completely get why you wouldn't want REPL clutter! Seeing `` splashed across your screen ruins the clean `1111` and `2222` output you're aiming for.
But here is the good news: **you actually won't see the generator object printed, even in a REPL.** Here is a reality check on how Python environments handle this:
### 1. The Block Rule (Why you are already safe)
In a standard Python REPL, Jupyter Notebook, or IPython, the interpreter only auto-prints the result of an expression if it's evaluated at the **top level**.
Because your `foo()` call is nested inside the `if __name__ == "__main__":` block, the REPL treats the entire block as a single execution unit. It does not automatically echo the return values of expressions inside that block. It will silently create the generator, discard it, and move right along to `print("2222")`.
### 2. Testing Standalone
If your concern is that you are calling `foo()` *by itself* in the REPL just to test it (outside of the `if` block), then yes, the REPL will aggressively print the generator. To suppress that, simply assign it to a throwaway variable:
```python
_ = foo()
```
---
### The "Chaotic Evil" Terminal Hack (No `yield` required)
If you absolutely despise the `yield` solution and want a completely different 1-line hack that isn't a generator, you can use an ANSI escape sequence.
Replace `xxxxxx` with:
```python
    return print("\033[1A\033[2K", end="")
```
**Why this works:**
1. Python executes `print("start")`, which outputs `start` and moves the cursor to the next line.
2. The `return print(...)` line executes.
3. `\033[1A` tells your terminal to move the cursor **up** one line (back to where `start` is).
4. `\033[2K` tells your terminal to **erase** that entire line.
5. `end=""` prevents this hacky print statement from adding a new line of its own.
6. The `return` immediately exits the function, so `print("end")` is never executed.
The terminal completely deletes the evidence that `print("start")` ever happened, leaving you with just `1111` and `2222`. *(Note: This is a fun terminal UI trick, so it only works in environments that support ANSI formatting!)*
uorz   
For script: yield
For repl: return print("\033[1A\033[2K", end="")
leiuu   
gemini 网页:直接答对✅
claude 客户端 sonnet 4.6:经过提示答对✅
chatgpt 网页免费版:经过提示答对✅
提示类似:“思路不对啊 有一个关键字可以做到“
uorz   
现在这个时代,面试这种 tricky 问题根本没有任何意义,跟以前 c 时代考各种指针写法一个道理。AI 比你我更了解各种语言细节。
CatCode   
审核代码的人:这么写代码的,还提交进代码库的,建议直接开了。
tianjiyao   
奇淫巧技不足道也。。。
lchkid   


啥 AI 不行啊?我测了下连豆包都行
您需要登录后才可以回帖 登录 | 立即注册

返回顶部