if I declare a const class which are contains variables:
for example
class const:
MASTER_CLIENT_CONNECTED = 0
CLIENT_CONNECTED = 1
GET_SERVER_QUEUE = 9998
ERROR = 9999
is there any way to reach this variable(constants) without creating a new class.
like this:
import const
const.MASTER_CLIENT_CONNECTED
or
import const
if(i==MASTER_CLIENT_CONNECTED):
Yes, instead of putting them in a class put them directly into your module (name the file e.g.
const.pyso the module name isconst). Using a class for this is pretty much abusing classes for namespacing – and Python has packages/modules for this purpose.Then you could even use
from const import *to import all globals from this module if you don’t want to writeconst.in front of everything. However note that this is usually discouraged as it potentially imports lots of things into the global namespace – if you just need one or two constantsfrom const import ABC, DEFetc. would be fine though.