Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can static variables be declared as private in python?

class Applicant:
    applicant_id_count=1000
    application_dict={
                        "A":0,
                        "B":0,
                        "C":0
                     }
    def __init__(self,applicant_name):
        self.__applicant_name=applicant_name
        self.__applicant_id=None
        self.__job_band=None

I need to make the static variables in the above class i.e. application_dict and applicant_id_count as private static variables. Or is there any such thing in python?

like image 450
Jazir Ahammed Avatar asked Oct 19 '18 15:10

Jazir Ahammed


People also ask

Can a static variable be declared private?

Just like an instance variables can be private or public, static variables can also be private or public.

Can a static method be private in Python?

In Python, there is no existence of Private methods that cannot be accessed except inside a class.

Is static variable public or private?

Static variables are created when the program starts and destroyed when the program stops. Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class. Default values are same as instance variables.

Can we use private with static?

Yes, we can have private methods or private static methods in an interface in Java 9. We can use these methods to remove the code redundancy. Private methods can be useful or accessible only within that interface only.


2 Answers

Python does not have access modifiers. If you want to access an instance (or class) variable from outside the instance or class, you are always allowed to do so.

That said there's a convention using underscores(_) that most developers follow to indicate that a variable/method is private. A single underscore is a convention of saying that it's a private variable but it actually doesn't change the access privilege. Example:

class Applicant:
    _applicant_id_count = 1000

Applicant._applicant_id_count # equals to 1000

If you want to emulate private variables for some reason, you can always use the __ prefix. Python mangles the names of variables so that they're not easily visible. Example:

class Applicant:
    __applicant_id_count=1000

You will get the following error when someone tries to directly access it:

Applicant.__applicant_id_count
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: class Applicant has no attribute '__applicant_id_count'

Someone can hack their way and use the variable like this:

Applicant._Applicant__applicant_id_count # prints out 1000

You can read more about it here: https://www.geeksforgeeks.org/private-variables-python/

like image 109
Bhargava Nandibhatla Avatar answered Nov 18 '22 06:11

Bhargava Nandibhatla


In Python, you can always access all variables. But, there is a convention for naming of this classes and attributes. You can use the __ prefix (two underscores) from PEP 8. Python mangles the names of variables like __foo so that they're not easily visible to code outside the class that contains them. Also, if you want a protected variable scope, you can use _ prefix (one underscore).

like image 23
Novak Avatar answered Nov 18 '22 06:11

Novak