More of my ideas for Python 3.16 (clear screen, apply ANSI color, try without except)


1: cls()
cls(). or ansi.cls(). or any way to intuitively clear the screen.
a lot of projects need frequent clearing of the terminal. 
and most of them can't use TUI libraries because they need full terminal control

programmers often have to do

os.system('cls' if os.name == 'nt' else 'clear')

i propose:

cls()

or:

ansi.cls()

2: try w/ implicit except
this sounds stupid, i know.
say i wanna simply catch an error, and immediately ignore it.
currently in python you have to do

try:
 riskything()
except ValueError:
 pass

or you have to do

try:
 riskything()
except:
 pass

i propose the following is implemented

try ValueError:
 riskything()

or

try:
 riskything()

so programmers have an easier time catching errors and doing nothing with them
3: color()
sometimes, you want to change your terminal color.
it happens a lot in TUI projects

and god help you if you want hexcodes.
you're gonna have to write a massive function with an inline ansi mess.

so, programmers usually need any obvious way.

i propose instead of doing
import sys

def color(hexcode: str):
    hexc = hexcode.strip().lstrip('#')
    if len(hexc) != 6 or not all(c in "0123456789abcdefABCDEF" for c in clean_hex):
        raise ValueError(f"bad hex color code: \"{hexcode}\"")
    r = int(hexc[0:2], 16)
    g = int(hexc[2:4], 16)
    b = int(hexc[4:6], 16)
    sys.stdout.write(f"\033[38;2;{r};{g};{b}m")
    sys.stdout.flush()
we simply do

color(0x00ff00)
one thing: i think my suggestions need heavy consideration and not an early cutoff
and another thing: no dogma because programming topics need arguments to be defensible engineering claims that you can defend and i can argue out
1 Like

Regarding 2, have you seen contextlib.suppress?

3 Likes

For 1 cls(), with Python 3.15, using the interpreter, try clear

The REPL, you mean.

it’s very frictionful to use such external libraries and i would prefer a builtin that uses the current known syntax

what if i dont have a repl and im making a hobby prohject

Example for:

...

socket_path = "./my-socket"

s = socket.socket(socket.AF_UNIX)

with contextlib.suppress(FileNotFoundError):
    os.unlink(socket_path)
s.bind(socket_path)

...

i get it but that’s a lot of friction compared to a shortened syntax sugar on try statements

The exception handling you propose seems like a less elegant version of the perchance keyword (Add `perchance`, a soft keyword for exception-tolerant expressions (PoC) by ambv · Pull Request #153767 · python/cpython · GitHub) which was unfortunately and inexplicably rejected.

1 Like

it isn’t perchance it’s just syntax sugar for

try: 
 something 
except: 
 pass

so we dont have to always type except: pass

There is also a statement form:

perchance FileNotFoundError, PermissionError: os.unlink(tmp)

which is sugar for try:/except (...): pass

from the link I “helpfully” provided

1 Like

well, the creator of perchance said, and i quote,

To sleep, perchance to dream.

To be clear, this is a joke. Please don’t close this before Pablo reviews it.

~ambv

mine is not a joke.

and i quote, another line,

which is sugar for try:/except (...): pass — the pattern the stdlib alone spells out ~1360 times.

~amvb

which means practically my try with implicit except:pass is a practical feature

Closing as this is the same low quality/effort as your previous topic, and unlikely to go anywhere productive. My ideas for Python 3.16 (goto and clear terminal)