Input() prompt doesn't always go to stdout

Following up:

The function as implemented is absurb.

The user does not only have to know whether readline was imported because their main imported A which imports B which imports C which imports readline, but after the release of Python 3.15, they may have to know whether a readline function was called. Here’a contrived example that will work differently on Tuesday than Sunday:

#!/usr/local/bin/python3.15
lazy import readline
import datetime
import os


def job():
    print("do something")

def weekend() -> bool:
    """Return True if Saturday or Sunday"""
    return datetime.date.today().weekday() >= 5


def prompt() -> bool:
    """Prompt user to verify they want to work on weekend. Check readline history to see
    if they have previously said yes. If so, skip prompt"""
    history_file = os.path.expanduser("~/.python_history")
    try:
        readline.read_history_file(history_file)
    except FileNotFoundError:
        pass

    PROMPT = "Work on weekend? (yes/no): "
    prefix = f"{PROMPT}"

    for i in range(readline.get_current_history_length(), 0, -1):
        item = readline.get_history_item(i)
        if item and item.startswith(prefix):
            return item[len(prefix):].strip().lower() == "yes"

    answer = input(PROMPT).strip().lower()
    if answer == "yes":
        readline.add_history(f"{prefix}{answer}")
        readline.write_history_file(history_file)
    return answer == "yes"
    

if weekend(): 
    print("weekend")
    if prompt():
        job()

_ = input("How are you feeling today?")

given:

  • breaking existing code is bad
  • if often doesn’t matter (if stderr and stdout are both left attached to the terminal)

A simple fix would be to add a file argument to input() to mirror the existing print() argument.

input(prompt,*,file=None)
so the user can just direct the prompt to where they want it.