I added a function to CPython (ternary operator)

Introduction

Here are the steps to add a ternary operator to CPython. This article uses Python 3.10. Please refer to here for the explanation of CPython build and file structure.

2. Addition of ternary operator

2.1 Python ternary operator

The Python ternary operators are: In Python, (value when condition is True) comes first, (condition) comes next, and finally (value when condition is False). It is not common for the (condition) to be in the middle in the ternary operator grammar.

(Value when the condition is True) if (conditions) else (conditionsがFalseのときの値)

2.2 Modified ternary operator

The modified ternary operator is as follows. (Condition) comes first, then (value when condition is True), and finally (value when condition is False). In C language etc., ternary operators are described in this order.


if (conditions) then (conditionsがTrueのときの値) else (conditionsがFalseのときの値)

2.3 Code change

Now let's change the Python ternary operator to the one shown in 2.2. The ternary operator already exists in Python. So, you can use this and call the same process when if then else comes. Here, we only change python.gram. There is the following description in L341 of Grammer / python.gram.

Grammer/python.gram


a = disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) }

This is a description of the original Python ternary operator. To imitate this, write the modified ternary operator on the line below.

Grammer/python.gram


'if' a = disjunction 'then' b=disjunction 'else' c=expression { _Py_IfExp(a, b, c, EXTRA) }

Here, pay attention to the order of the variables when calling _Py_IfExp. With the above changes

$ make regen-pegen

You can add the ternary operator by executing and rebuilding. Of course, you can also use the original ternary operator syntax.

3. Demo

The ternary operator implemented by the above changes works as follows. Peek 2020-10-20 13-05.gif

Reference material

-Exploring large-scale software
This is an experimental site.

-CPython
CPython repository

-24. Changing CPython ’s Grammar
A checklist for adding features to CPython was included to help you understand the structure of CPython.

Recommended Posts

I added a function to CPython (ternary operator)
I added a function to CPython (build & structure grasp)
How to call a function
I made a function to check the model of DCGAN
Added achievement function to Sublime Text
How to make a recursive function
I wrote a function to load a Git extension script in Python
I tried to create a linebot (implementation)
I want to print in a comprehension
I tried to create a linebot (preparation)
I had turtle draw a trigonometric function
I want to build a Python environment
A simple IDAPython script to name a function
I tried adding post-increment to CPython Implementation
Added a function to register desired shifts in the Django shift table
I made a script to display emoji
I tried to make a Web API
Python: I want to measure the processing time of a function neatly
I made a function to see the movement of a two-dimensional array (Python)
I tried to make a dictionary function that does not distinguish between cases
[Python] I tried to get the type name as a string from the type function
I want to make matplotlib a dark theme
I tried to build a super-resolution method / ESPCN
I want to easily create a Noise Model
To execute a Python enumerate function in JavaScript
I want to INSERT a DataFrame into MSSQL
How to create a function object from a string
I tried to build a super-resolution method / SRCNN ①
I want to create a window in Python
I want to make a game with Python
I don't want to take a coding test
I made a tool to compile Hy natively
I want to create a plug-in type implementation
I read "How to make a hacking lab"
I wrote a script to upload a WordPress plugin
I tried to generate a random character string
I tried to build a super-resolution method / SRCNN ③
I tried to build a super-resolution method / SRCNN ②
I made a tool to get new articles
I tried adding post-increment to CPython Extra edition
I want to easily find a delicious restaurant
[Go] How to write or call a function
I want to write to a file with Python
How to Mock a Public function in Pytest
I want to use the activation function Mish
I tried to make a ○ ✕ game using TensorFlow
I want to upload a Django app to heroku
Ternary operator (Python)
I tried to learn the sin function with chainer
I tried to make a "fucking big literary converter"
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I tried to create a table only with Django
I want to iterate a Python generator many times
Create a function to visualize / evaluate the clustering result
I want DQN Puniki to hit a home run
100 image processing knocks !! (021-030) I want to take a break ...
I want to give a group_id to a pandas data frame
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to transition with a button in flask
I made a library to separate Japanese sentences nicely