Working with constants

This is a small part of pygame’s constant list. Needed for keyboard events checking if a key was pressed for example. The name of the constant = K_BACKSPACE is this changeable at will? Or are they set by pygame and are to be used in that format

#  pygame constants

'''
Constant      ASCII   Description
---------------------------------
K_BACKSPACE   \b      backspace
K_TAB         \t      tab
K_CLEAR               clear
K_RETURN      \r      return
K_PAUSE               pause
K_ESCAPE      ^[      escape
K_SPACE               space
K_EXCLAIM     !       exclaim
K_QUOTEDBL    "       quotedbl

Thanks, I don’t want to cause myself problems when running the script later.

pygame.constants is an extension module that’s written in C. The names of the key constants are based on SDL (Simple DirectMedia Layer) constants from SDL_keycode.h, e.g. SDLK_QUOTEDBL -> K_QUOTEDBL.

Since Python modules don’t prevent setting and deleting attributes, there’s nothing stopping you from modifying the constants module. For example:

>>> from pygame import constants
>>> constants.K_DOUBLE_QUOTE = constants.K_QUOTEDBL
>>> del constants.K_QUOTEDBL

But you shouldn’t do this. Changing the names of the constants will break code that relies on the documented names. Even if you just set aliases for use in your own code, that’s making life more difficult than it has to be for anyone who reads your code who already knows pygame and/or SDL.