0

The first part is what I want to do and the questions. Before discussing why I want to to that and proposing counterarguments, please read the motivation in the second part.

In short: I am not a developer. The main use of Python is fast prototyping of mathematical methods; additional motivation is learning how Python is implemented. This topic is not of crucial importance for me. If it seems lame and off-topic, feel free to remove, and I apologize for the inconvenience.

This feature does not introduce new functionality but serves as a shortcut for lambda. The idea is borrowed from .

If the symbol of closing parenthesis is preceded with &, then the code inside the parentheses is interpreted as the definition of a function, where `1, `2, ... play the role of its arguments. Example: (`1 + `2 &)(a, b) means (lambda x, y: x + y)(a, b)

  1. Provided that I learn everything needed about Python, how hard / time consuming is to implement that extension? At the moment, I see two options:
    1.a. Preprocessing text of the script before compiling (I use iPython in Anaconda).
    Immediate problem: assigning unique names to arguments. Possible workaround: reserve names such as __my_lambda_123.

    1.b. Modifying CPython similarly as described in https://hackernoon.com/modifying-the-python-language-in-7-minutes-b94b0a99ce14

  2. Imagine that I implemented that feature correctly. Do you immediately see that it breaks something essential in Python, or iPython, or Anaconda? Assume that I do not use any developers' packages such as unittest, but a lot of "scientific" packages including numpy, as well as "interface" packages such as sqlalchemy.

Motivation. I gradually study Python as a programming language and appreciate its deepness, consistency and unique philosophy. I understand that my idea is not in line with the latter. However, I use Python for implementing mathematical methods which are barely reusable. A typical life cycle is following: (1) implement some mathematical method and experiments for a research project; (1.a) maybe save some function/class in my package if it feels reusable; (2) conduct computational experiments; (3) publish a paper; (4) never use the code again. It is much easier to implement an algorithm from scratch than to structure and reuse the code since the coincidence between large parts of different methods is very rare.

My typical project is one large Python script with long code fragments. Even structuring the code into functions is not time-efficient, since the life cycle of my program does not include "deploy", "maintain", "modify". I keep the amount of structure to a minimum needed for fast implementing and debugging.

I would use but in my recent projects, it became useless due to the limitations of its standard libraries, the poor performance of the code in Wolfram language, and overall closeness of the platform. I switch to Python for its rich selection of libraries, and also with the intent of acquiring some software developer's skills. However, at the moment, programming in the Wolfram language style is much more efficient for me. The code of algorithms feels much more readable when it is more compact (you do not need to scroll), and includes less language-specific words such as lambda.

James
  • 32,991
  • 4
  • 47
  • 70
pdp
  • 51
  • 6
  • 1
    For your use I think sticking with the standard Python syntax is best. Even if you manage to tweak `CPython` to add this new syntax, what happens when a new version is released? You have to port your modifications to that, giving yourself a lot more work each time. – hpaulj Jun 01 '20 at 15:00

1 Answers1

4

Just as a heads up, the issue you raise in 1a is called macro hygiene.

It's also a bit sketchy to be doing the "lambda replacing" in text, before it's converted to an abstract syntax tree (AST). This is certainly going to be prone to errors since now you have to explicitly deal with various parsing issues and the actual replacement in one go.

If you do go this route (I don't recommend it), I recommend you also look at Racket's macro system, which can do what you want.

There are also other potential problems you might run into - you need to think about how you want strings such as ("`1" + `1)(a) to parse, or, for example, strings such as (`2 + `3)(a, b) - is this an error or is it ok (if so, which argument goes where?). These are the kinds of test cases you need to think about if you're sure you want to design an addition to Python's syntax.

There's also a practical consideration - you'll essentially need to support your own fork of Python, so you won't be able to get updates to the language without redeveloping this feature for each release (kind of? I think).

TLDR: I highly recommend you don't do this, just use lambdas.

MLavrentyev
  • 1,827
  • 2
  • 24
  • 32
  • 1
    Thanks a lot for quick reply and useful info. I also dislike the fact that it would interfere with debugging. The only reason for me to consider it: seems much easier to implement, much shallower level of CPython knowledge is needed. – pdp Jun 01 '20 at 12:24