Skip to content

Instantly share code, notes, and snippets.

@benadaba
Last active September 8, 2018 14:49
Show Gist options
  • Save benadaba/f9a5daf60679496850ba08c2c15c749a to your computer and use it in GitHub Desktop.
Save benadaba/f9a5daf60679496850ba08c2c15c749a to your computer and use it in GitHub Desktop.
Public Protected Private Variables
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In Python, the scope ( Public, Protected, Private) characteristic of an attribute or member of the class is indicated by \"the naming conventions of the member\". These are the naming conventions\n",
"\n",
"**Public**: This means the member can be accessed outside of the class by other instances. \n",
" The naming convention denotes that it has no underscores eg numberOfFields = 6\n",
"**Protected**: This means that the member can be accessed by the Parent Class and its \"Children\" - those classes that inherit the parent class\n",
" The naming convention denotes that it has **ONE underscore in front of the member name** eg _numberOfFields = 6\n",
"**Private** This means the member is only accessible within the parent class. \n",
" The naming convention denotes that it has TWO underscores in front of the member name eg __numberOfFields = 6\n",
" \n",
" \n",
"Lets check some examples"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"class Car:\n",
" numberOfWheels = 4\n",
" _colour = \"Black\" #protected attribute\n",
" __yearOfManufacture = '2017' ## private"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"class BMW(Car):\n",
" def __init__(self):\n",
" print(\"Protected attribute colour of car is :{} and the car has {}\".format(self._colour, self.numberOfWheels))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets instantiate both classes and see how the attributes behave"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Public attribute 4\n"
]
}
],
"source": [
"car = Car()\n",
"print(\"Public attribute {}\".format(car.numberOfWheels))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Protected attribute colour of car is :Black and the car has 4\n"
]
}
],
"source": [
"bmw = BMW()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets access the private variable outside of the class"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "Car instance has no attribute '__yearOfManufacture'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-21-f33c89bb066a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Accessing the private variable: \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcar\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__yearOfManufacture\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m: Car instance has no attribute '__yearOfManufacture'"
]
}
],
"source": [
"print(\"Accessing the private variable: \", car.__yearOfManufacture)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Though we are strongly advised not to use the private variable outside of the class we can actually access it like below"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('Accessing the private variable YearOfManufacture: ', '2017')\n"
]
}
],
"source": [
"print(\"Accessing the private variable YearOfManufacture: \", car._Car__yearOfManufacture)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment