Created
March 13, 2020 12:07
-
-
Save dfbarrero/a6ebc6632fb9fe06a2c8c6a47a465ab5 to your computer and use it in GitHub Desktop.
Example of private attributes in Python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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