Skip to content

Instantly share code, notes, and snippets.

@dfbarrero
Created March 13, 2020 12:07
Show Gist options
  • Select an option

  • Save dfbarrero/a6ebc6632fb9fe06a2c8c6a47a465ab5 to your computer and use it in GitHub Desktop.

Select an option

Save dfbarrero/a6ebc6632fb9fe06a2c8c6a47a465ab5 to your computer and use it in GitHub Desktop.
Example of private attributes in Python.
class Dog:
def __init__(self):
self.__name = "Unknown"
self.__age = 10
def setName(self, name):
self.__name = name
def getName(self):
return self.__name
def setAge(self, age):
if age < 20:
self.__age = age
def getAge(self):
return self.__age
if __name__ == '__main__':
snoopy = Dog()
snoopy.setName("Snoopy")
print(snoopy.getName())
print(snoopy.__name) # Error!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment