features I would like to see in python

Some python features I have looked for, but do not exist.

  • assignment operator
  • class setup
  • conditional assignment
  • boolean inplace operators
  • byte() conversion

assignment operator

Kind of like permanently binding a type to a variable.

class Object:
    def __assign__(self, value):
        print("assigned")

>> a = Object()
>> a = 1
assigned

class setup

class Object:
    def __setup__(cls):
        cls.CONSTANT = 123
        cls.method = lambda x: x+1

__setup__ will be called only once in the existence of the class Object.

maybe the qualname variable can help.

declare class constants

Hmm, I wrote this down but I forgot what I meant with this, this is basically just three different ways of declaring class constants.

def addconstant(name, value):
    import inspect
    inspect.currentframe().f_back.f_locals[name] = value
class Tst:
    addconstant('xyz', 123)
    locals()['klm'] = 123
    abc = 123

print Tst.xyz
print Tst.klm
print Tst.abc

conditional assignment

It would be nice to have a way to avoid having to write this:

if a is None:
    a = b

something like:

a ?= b    #  assigns only when a is None

boolean inplace operators

Most operators have an ‘inplace’ variant, but not the boolean operators:

a or= b
a and= b

byte() conversion

A function which converts an integer to a single byte. like chr() converts an integer to a character.