发现 Python 一个有意思的小特性,发现很合适搞成面试题。问了 AI 都不行:),欢迎来挑战~ - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
xFrank
V2EX    Python

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

  •  
  •   xFrank 5 小时 7 分钟前 1833 次点击

    已知如下 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 的不行) 

    引申问题:上述替换代码要求放宽为“允许引入自带库并允许多条语句”,是否还有别的方案?

    第 1 条附言    1 小时 12 分钟前
    我开始是发在“好玩”板块的,大家当个乐子就行,忽略“面试”。。。
    31 条回复    2026-04-01 14:37:26 +08:00
    uorz
        1
    uorz  
       5 小时 2 分钟前 via Android
    Gemini: yield.
    maolon
        2
    maolon  
       4 小时 56 分钟前


    你确定你问对了 ai ?
    uorz
        3
    uorz  
       4 小时 55 分钟前 via Android
    I completely get why you wouldn't want REPL clutter! Seeing `<generator object foo at 0x...>` 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
        4
    uorz  
       4 小时 53 分钟前 via Android
    For script: yield
    For repl: return print("\033[1A\033[2K", end="")
    leiuu
        5
    leiuu  
       4 小时 41 分钟前
    gemini 网页:直接答对
    claude 客户端 sonnet 4.6:经过提示答对
    chatgpt 网页免费版:经过提示答对

    提示类似:“思路不对啊 有一个关键字可以做到“
    uorz
        6
    uorz  
       4 小时 32 分钟前 via Android   1
    现在这个时代,面试这种 tricky 问题根本没有任何意义,跟以前 c 时代考各种指针写法一个道理。AI 比你我更了解各种语言细节。
    CatCode
        7
    CatCode  
       4 小时 22 分钟前
    审核代码的人:这么写代码的,还提交进代码库的,建议直接开了。
    tianjiyao
        8
    tianjiyao  
       4 小时 18 分钟前
    奇淫巧技不足道也。。。
    lchkid
        9
    lchkid  
       4 小时 11 分钟前


    啥 AI 不行啊?我测了下连豆包都行
    yuruizhe
        10
    yuruizhe  
       4 小时 11 分钟前
    这纯考 python 语法细节啊
    不涉及任何算法
    下次可以整个考查装饰器的题目
    weixind
        11
    weixind  
       4 小时 0 分钟前
    你们面试目的是为了考倒候选人?
    whoosy
        12
    whoosy  
       3 小时 57 分钟前   5
    何意味?麻烦你说下公司名,我避下雷
    blakezhaothinks
        13
    blakezhaothinks  
       3 小时 49 分钟前
    这种奇技淫巧每种语言都有很多,拿来做面试题属实逆天,就算 CPython 核心开发者也不一定了解每个细节。
        14
    blakezhaothinks  
       3 小时 46 分钟前
    @blakezhaothinks 不过 yield 也算不上奇技淫巧,当我没说
    soleils
        15
    soleils  
       3 小时 45 分钟前
    你面试是为了招人来干活还是为了出些"奥数"题目把人考倒看乐子?
    xFrank
        16
    xFrank  
    OP
       3 小时 39 分钟前
    我错了,给大家带来了不好的影响,麻烦管理员删除帖子。怎么 @管理员?
    uorz
        17
    uorz  
       3 小时 37 分钟前 via Android
    我相信你内心是希望面试人回答 yield 的。经过提示,Gemini 还给出了`[__import__('os').system('cls' if __import__('os').name == 'nt' else 'clear'), print("1111"), __import__('sys').exit(print("2222"))]`这样的答案。是不是面试人没回答到心坎里去就 pass 掉?
    kaliawngV2
        18
    kaliawngV2  
       2 小时 41 分钟前 via Android
    @xFrank 你也不希望被 v 友骂吧,v 我 50 ,我帮你删帖
    deplives
        19
    deplives  
       2 小时 17 分钟前
    emmmm 用这个当面试题,赶紧发下公司名给网友避避雷
    back0893
        20
    back0893  
       2 小时 16 分钟前
    ? 这种面试题是为了什么?
    jackOff
        21
    jackOff  
       2 小时 14 分钟前
    ? 这种面试题是为了什么?
    fkdtz
        22
    fkdtz  
       1 小时 39 分钟前   1
    我承认挑战失败,憋笑挑战
    mynameislihua
        23
    mynameislihua  
       1 小时 35 分钟前 via Android
    招个人机去你们公司
    bu9
        24
    bu9  
       1 小时 32 分钟前
    你居然想用八股难为 ai ,还不认为自己想法有问题?
    mrji1990
        25
    mrji1990  
       1 小时 31 分钟前
    毫无意义
    Vaspike
        26
    Vaspike  
       1 小时 31 分钟前
    面试题的角度来说确实不太合理, 但对我来说我确实学习到了一个 py 语法
    hackerfans
        27
    hackerfans  
       1 小时 18 分钟前
    get
    Gilfoyle26
        28
    Gilfoyle26  
       1 小时 14 分钟前
    opus 4.6:总有人觉的自己比我聪明,结果就是啪啪打脸。
    xFrank
        29
    xFrank  
    OP
       1 小时 10 分钟前
    @uorz #1 我啥我问 Gemini 就不行呢。。。
    NoobPhper
        30
    NoobPhper  
       1 小时 10 分钟前
    你门公司叫啥啊
    xFrank
        31
    xFrank  
    OP
       42 分钟前
    @NoobPhper 碳基生物静息能效管理中心
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5505 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 28ms UTC 07:20 PVG 15:20 LAX 00:20 JFK 03:20
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86