Do classes in Python have access modifiers for their methods and class variables?

In regards to the Python 3 course, in the “Classes” section, we learn about creating classes, class methods, and class variables.

Many programming languages have access modifiers (keywords like public, private, protected. Also static) for specifying the level of access on a method or attribute. Is there anything similar in Python?

Yes, Python does have access modifiers that allow you to specify the level of access on methods and attributes of a class. Python uses naming conventions to indicate the intended visibility of methods and attributes. I will attach a code block down below so you can understand better.

class MyClass:
    def __init__(self):
        self.public_var = 52
        self._protected_var = "Hi"
        self.__private_var = True
    def public_method(self):
        print("This is a public method.")

    def _protected_method(self):
        print("This is a protected method.")

    def __private_method(self):
        print("This is a private method.")

obj = MyClass()
print(obj.public_var)    # Accessing public variable
obj.public_method()      # Calling public method

print(obj._protected_var)    # Accessing protected variable (convention)
obj._protected_method()      # Calling protected method (convention)
1 Like

I see. So the number of underscores represents the level of access. Thank you very much! Do you by any chance know how I would represent a static variable or method?

1 Like

While Python does not have a separate keyword or modifier for static variables you can define static variables directly within the class but outside any method. These variables are shared among all instances of the class and can be accessed using the class name or an instance of the class. Also remember there are no difference between class_variable and static_variable in python.

class MyClass:
    static_var = 52

    def __init__(self, instance_var):
        self.instance_var = instance_var

print(MyClass.static_var)    # Accessing static variable using the class name
obj1 = MyClass(1)
obj2 = MyClass(2)
print(obj1.static_var)       # Accessing static variable using an instance

Now to define a static method within a class, you can use the @staticmethod decorator. Like:

class MyClass:
    def static_method():
        print("This is a static method.")

obj = MyClass()

Just a curious question, Have you studied Java before learning Python?


First of all, thank you very much for the explanations! Now everything is clear.
Secondly, I studied both Java and C# before and did an OOP course on C# in university that was pretty in-depth. I see Python is not as strict as these two languages but still manages to impose their functionalities in an elegant way.

1 Like

Hey, it’s okay I’m happy you understood what I wanted to explain you.

Oh, I see I did Java with OOPs in beginning and when I shifted to python I was asking myself these same questions. Well this also shows you did your work perfectly before. Good luck learning python @ioan-alexandrugheorg. I started learning python for my Data science classes and I really wanna learn backend with python.

1 Like

Thank you my friend! Good luck to you too in learning backend with Python! :smiley:

1 Like