Files
learn-python3/notebooks/beginner/classes.ipynb
Jerry Pussinen 46450175dc Initial commit
2018-05-01 13:59:37 +02:00

168 lines
4.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# [Classes](https://docs.python.org/3/tutorial/classes.html#a-first-look-at-classes)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class MyFirstClass:\n",
" def __init__(self, name):\n",
" self.name = name\n",
"\n",
" def greet(self):\n",
" print('Hello {}!'.format(self.name))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_instance = MyFirstClass('John Doe')\n",
"print('my_intance: {}'.format(my_instance))\n",
"print('type: {}'.format(type(my_instance)))\n",
"print('my_instance.name: {}'.format(my_instance.name))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Methods\n",
"The functions inside classes are called methods. They are used similarly as functions. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"alice = MyFirstClass(name='Alice')\n",
"alice.greet()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `__init__()`\n",
"`__init__()` is a special method that is used for initialising instances of the class. It's called when you create an instance of the class. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Example:\n",
" def __init__(self):\n",
" print('Now we are inside __init__')\n",
" \n",
"print('creating instance of Example')\n",
"example = Example()\n",
"print('instance created')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`__init__()` is typically used for initialising instance variables of your class. These can be listed as arguments after `self`. To be able to access these instance variables later during your instance's lifetime, you have to save them into `self`. `self` is the first argument of the methods of your class and it's your access to the instance variables and other methods. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Example:\n",
" def __init__(self, var1, var2):\n",
" self.first_var = var1\n",
" self.second_var = var2\n",
" \n",
" def print_variables(self):\n",
" print('{} {}'.format(self.first_var, self.second_var))\n",
" \n",
"e = Example('abc', 123)\n",
"e.print_variables()\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Class variables vs instance variables\n",
"Class variables are shared between all the instances of that class whereas instance variables can hold different values between different instances of that class."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Example:\n",
" # These are class variables\n",
" name = 'Example class'\n",
" description = 'Just an example of a simple class'\n",
"\n",
" def __init__(self, var1):\n",
" # This is an instance variable\n",
" self.instance_variable = var1\n",
"\n",
" def show_info(self):\n",
" info = 'instance_variable: {}, name: {}, description: {}'.format(\n",
" self.instance_variable, Example.name, Example.description)\n",
" print(info)\n",
"\n",
"\n",
"inst1 = Example('foo')\n",
"inst2 = Example('bar')\n",
"\n",
"# name and description have identical values between instances\n",
"assert inst1.name == inst2.name == Example.name\n",
"assert inst1.description == inst2.description == Example.description\n",
"\n",
"# If you change the value of a class variable, it's changed across all instances\n",
"Example.name = 'Modified name'\n",
"inst1.show_info()\n",
"inst2.show_info()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}