A
A
Alexander Rublev2016-08-10 17:51:25
Python
Alexander Rublev, 2016-08-10 17:51:25

Is it bad to use one global constant?

Hello.
I am writing a program in which I have a login and password for each user + access rights.
Here is my question. I need to store the user's access rights throughout the program.
Access rights are used in different function classes and program files.
It seems to me that the simplest solution is to create one global constant in which access rights will be stored.
But I heard that global variables are evil. But do not pass a variable from class to class that will say "admin" or "work".
Tell me what do you think about this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
theg4sh, 2016-08-10
@Meller008

Use a construct like:

class Auth:
  _instance = None;
  def __init__(self, user, pswd):
    # some initialization
    self._id = None
    pass
  def getId(self):
    return self._id
  @staticmethod
  def getInstance(user, pswd):
    if Auth._instance is None:
      Auth._instance = Auth(user, pswd)
    return Auth._instance

# later use authorization as Auth.getInstance().getId();

In general, it is worth getting acquainted with the concept of Singleton and the contents of this link Upd
: an example example, but I corrected the initialization, I'm sorry :)
Upd2: the getInstance method must be static in order to satisfy the Auth.getInstance() usage condition. Corrected.

R
Rou1997, 2016-08-10
@Rou1997

Static Config class with static fields.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question