Skip to content Skip to sidebar Skip to footer

How Do I Make Private Variables Inaccessable In Python?

class Car(object): def __init__(self, color, engine, oil): self.color = color self.__engine = engine self.__oil = oil a = Car('black', 'a cool engine',

Solution 1:

The problem is simple. I want private variables to be accessed and changed only inside the class.

So, don't write code outside the class that accesses variables starting with __. Use pylint or the like to catch style mistakes like that.

Solution 2:

What you are trying to do is not possible in Python.

“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python.

https://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references

Post a Comment for "How Do I Make Private Variables Inaccessable In Python?"