# A Python puzzle

DevFeed: [A Python puzzle](<https://devfeed.tech/articles/a-python-puzzle-29432.md>)

Original publisher: [Read original article](<http://akaptur.github.com/blog/2013/10/29/a-python-puzzle/>)

Published: 2013-10-30T02:40:00Z

Content type: article

Language: en

Sources: [Allison Kaptur](<https://devfeed.tech/sources/allison-kaptur.md>)

Topics: [Python](<https://devfeed.tech/topics/python.md>), [Code](<https://devfeed.tech/topics/code.md>), [function](<https://devfeed.tech/topics/function.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [function](<https://devfeed.tech/tags/function.md>), [puzzle](<https://devfeed.tech/tags/puzzle.md>), [python](<https://devfeed.tech/tags/python.md>), [rules](<https://devfeed.tech/tags/rules.md>), [stateful](<https://devfeed.tech/tags/stateful.md>)

## AI overview

A Python puzzle challenges readers to find three valid Python expressions that produce different results when run separately, on one line with semicolons, and inside a function. The solution must avoid introspection, dunder methods, order dependence, and stateful behavior.

## Source excerpt

A couple of Hacker Schoolers were discussing an interesting corner of python today. We discovered a nice bit of trivia: there exist three lines of python code that display the following behavior: 1 2 3 4 5 6 7 8 9 10 11 12 >>> LINE_A >>> LINE_B >>> LINE_C False >>> LINE_A; LINE_B; LINE_C True >>> def my_function(): ... LINE_A ... LINE_B ... LINE_C >>> my_function() True What are the lines? Some ground rules: Introspection of any kind is cheating (e.g. noting the line number). No dunder (__foo__) methods allowed. Each line is a valid python expression. You can't rely on order: while the lines will always execute A -> B -> C, a complete solution behaves identically if e.g. the semicolon version happens before the separate-line version. No cheating with the function: e.g. you can't add a return unless you add it everywhere. Edit: And nothing stateful. For bonus points, code golf! My solution to this is 14 19 characters long, not counting whitespace.