Metaclass Re-Implementation

For Standards Track; PEP 887 or any free in 8xx
Author: Aveyzan <aveyzan at gmail.com>
Created 20th August 2025

Adding a metaclass to a class is simple:

$k[class] $c[MyClassMeta]: $o[...] $k[class] $c[MyClass]($p[metaclass] $o[=] $c[MyClassMeta]): $o[...]

Receive the metaclass using this technique:

$c[type]($c[MyClass]) # <class 'MyClassMeta'>

Python 2 used version with the __metaclass__ attribute, which is no longer supported on Python 3. This answer on Stack Overflow would flow nicely to definition of a metaclass.

My suggestion is providing either metaclass or meta keyword that only take place when it is used in inheritance section. Otherwise, this will emit environment error. Version with = I consider nice, but I think it may be misleading, because the operator is used for default values in functions.

$k[metaclass] $c[MyClassMeta] # incorrect $k[class] $c[MyClass]($k[metaclass] $c[MyClassMeta]): $o[...] # correct

This keyword isn't completely restricted. Only when there is a class next to this keyword (separated by at least one space), this keyword takes its effect. That means this will be correct:

$v[metaclass] $o[=] $n[72] # any arbitrary value

But this keyword itself may be completely reserved! I don't mind, if this metaclass would be assigned to Python 2 attribute __metaclass__, but I do think it is a good concept to begin with.

Asynchronous Generator Expressions

For Standards Track; PEP 888 or any free in 8xx
Author: Aveyzan <aveyzan at gmail.com>
Created 20th August 2025

The only ways to receive an asynchronous generator are:

PEP 289 introduced generator expressions, and they exist since Python 2.4, but I think there should be async generator expression equivalent as well. Consider the following syntax:

$v[asyncgen] $o[=] $k[async] ($v[i] $k[for] $v[i] $k[in] <iterable>)

This would be the same as:

$k[async] $k[def] $f[asyncgen_]($p[iterable]):   $k[for] $v[i] $k[in] $p[iterable]:     $k[yield] $v[i] $v[asyncgen] $o[=] $f[asyncgen_](<iterable>)

Version with condition is the same as in mere generator expression, just including the async keyword:

$v[asyncgen] $o[=] $k[async] ($v[i] $k[for] $v[i] $k[in] <iterable> $k[if] <condition>)

This would be the same as:

$k[async] $k[def] $f[asyncgen_]($p[iterable], $p[condition]):   $k[for] $v[i] $k[in] $p[iterable]:     $k[if] $p[condition]($v[i]):       $k[yield] $v[i] $v[asyncgen] $o[=] $f[asyncgen_](<iterable>, <condition>)

It is invalid for using async keyword preceding a list/set/dictionary comprehension. When convering an asynchronous generator object from asynchronous generator expression, you would receive one of the following:

AveyTense in version 0.3.53 provides class method aveytense.Tense.asyncGenerator() to prevent creating a function directly to invoke later to receive asynchronous generator object.

Allow yield from in Asynchronous Generator Functions

For Standards Track; PEP 889 or any free in 8xx
Author: Aveyzan <aveyzan at gmail.com>
Created 20th August 2025

yield from, introduced by PEP 380 for Python 3.3, can be applied only to non-asynchronous generator functions:

$k[def] $f[gen_]($p[iterable]):   $k[yield] $k[from] $p[iterable] # valid $k[def] $f[asyncgen_]($p[iterable]):   $k[yield] $k[from] $p[iterable] # invalid

The const keyword

For Standards Track; PEP 890 or any free in 8xx
Author: Aveyzan <aveyzan at gmail.com>
Created 20th August 2025

De facto almost every variable in Python is re-assignable, and sometimes we would want, as in JavaScript, to prevent this. JavaScript already offers it with the const keyword:

$k[const] $v[value] $o[=] $n[72]; $v[value] $o[=] $n[84]; // TypeError: Assignment to constant variable

In Python, especially in typing, there is special class typing.Final to prohibit variable re-assignment. Well, PEP 591 doesn't work completely, it is only on need for typing.

The closest to this approach in normal coding are presumably descriptors, or using __setattr__() and __delattr__() to emit an error, for example: tuples and enum.Enum class subclasses.

Because it is difficult to make a variable constant not being a class attribute, the const keyword would find place only in classes... and their names would be assigned to an internal attribute preferably being a tuple to make it work.

Syntax with the const keyword will be practically the same as in JavaScript, just without semicolon:

$k[const] <constant_name> $o[=] <value>

It is an error without the = operator. In C this would be equal to:

$k[const] <type/$k[auto]> <constant_name> $o[=] <value>;

and just like in JavaScript, attempt to re-assign a constant raises an error. Macro definition in C:

$o[#]$k[define] $con[PyAPI_Const]($p[name], $p[value]) $k[const] $p[name] $o[=] $p[value]

In C type of the value would be simply evaluated from Python's type class constructor as type(value).
Site created by Aveyzan on 31st July 2022