Last active
January 28, 2016 13:30
-
-
Save ishankhare07/0a9eb008f1c0642909c0 to your computer and use it in GitHub Desktop.
Class variables and __init__ function details
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
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:c70435e7bc348ba3ce355458022d15d1b71c2441dac4907faeb275bf6cdc4b5d" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Test:\n", | |
" \"\"\"demonstrating option initialization\"\"\"\n", | |
" def __init__(self, data=0):\n", | |
" self.data = data\n", | |
" \n", | |
" def __repr__(self):\n", | |
" \"\"\"overloading default print and repr function\"\"\"\n", | |
" return \"<Class Test, data={0}>\".format(self.data)\n", | |
"\n", | |
"t = Test() # uninitialized\n", | |
"t2 = Test(10)\n", | |
"\n", | |
"print('t ->', t)\n", | |
"print('t2 ->', t2)\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"('t ->', <Class Test, data=0>)\n", | |
"('t2 ->', <Class Test, data=10>)\n" | |
] | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Above is the output of the program\n", | |
"\n", | |
"The next example demonstrates the difference between `class` and `instance` variables" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Demo:\n", | |
" classvar = 10\n", | |
" \n", | |
" def __init__(self):\n", | |
" self.data = 10\n", | |
"\n", | |
" def __repr__(self):\n", | |
" return \"<Class Demo, classvar={0}, data={1}>\".format(Demo.classvar, self.data)\n", | |
"\n", | |
"a = Demo()\n", | |
"b = Demo()\n", | |
"\n", | |
"print(a)\n", | |
"print(b)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"<Class Demo, classvar=10, data=10>\n", | |
"<Class Demo, classvar=10, data=10>\n" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"now we change the `classvar` in and try to print both `a` and `b` again" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"Demo.classvar = 50\n", | |
"\n", | |
"print(a)\n", | |
"print(b)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"<Class Demo, classvar=50, data=10>\n", | |
"<Class Demo, classvar=50, data=10>\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"we see that **class variable** change reflects in __both instances__ `a` and `b`\n", | |
"\n", | |
"Next we try to change `data` which is and __instance variable__ from `a` and then print again" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a.data = 100\n", | |
"\n", | |
"print(a)\n", | |
"print(b)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"<Class Demo, classvar=50, data=100>\n", | |
"<Class Demo, classvar=50, data=10>\n" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"as we see, instance variables reflect changes in **only** that particular instance\n", | |
"\n", | |
"whereas the changes to *class variables* are reflected across __all__ the **instance variables**\n", | |
"\n", | |
"next we try to change classvar from __init__ using optional arguments" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Demo:\n", | |
" \"\"\"\n", | |
" This will override (redefine) the previous Demo class\n", | |
" \"\"\"\n", | |
" \n", | |
" def __init__(self, classvar_value=50, instance_value = 10):\n", | |
" Demo.classvar = classvar_value\n", | |
" # classvar only comes into existence after the above line is executed\n", | |
" # i.e. after at least 1 instance is created\n", | |
" \n", | |
" self.data = instance_value\n", | |
" \n", | |
" def __repr__(self):\n", | |
" return \"classvar={0}, data={1}\".format(Demo.classvar, self.data)\n", | |
"\n", | |
"a = Demo()\n", | |
"b = Demo(100)\n", | |
"c = Demo(100, 100)\n", | |
"\n", | |
"print(a, b, c, sep='\\n')\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"classvar=100, data=10\n", | |
"classvar=100, data=10\n", | |
"classvar=100, data=100\n" | |
] | |
} | |
], | |
"prompt_number": 2 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment