[Python] Most elegant way to port C code with ++ and -- operators to Python [closed]

Discussão em 'Python' iniciado por Stack, Fevereiro 11, 2025.

  1. Stack

    Stack Membro Participativo

    Motivation: I am a senior software engineer who is developing an Intro to CS and Programming Language Design course. Part of my curriculum is centered around a cleaned-up TinyBASIC interpreter originally written in C.

    The problem: I probably want to teach the course in Python, alas, Python has no macros, and, philosophy aside, ++ and -- operators are both convenient, readable, and concise. (By comparison, porting the C code to Common Lisp as a stepping-stone was easy, because Lisp has an awesome macro "preprocessor.")

    Question: Is there any elegant way to express pre-/post-increment, and pre-/post-decrement concepts in Python? Yes, of course I can separate out these operations into separate assignment statements, but it would double the size of the code in some places, while making the code only marginally more readable. And if the size of the code doubles, it will be harder to fit onto lecture slides. (Extreme brevity is one of the reasons I was attracted to the coding style of the original TinyBASIC interpreter).

    Footnote: When I say "elegantly" I mean: without introducing, say, objects where simple, scalar data types would suffice. Remember, I will have to explain to the students any programming techniques I use to get around the absence of macros.

    Continue reading...

Compartilhe esta Página