"#Python is an object-oriented language. This allows us to bundle the properties (data) and behaviors (functions) into individual objects, which make them easy to reuse and build upon.\n",
"#data and functions associated with a specific class are called \"attributes\" and \"methods\", respectively."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.NiceCar object at 0x112360f98>\n",
"<__main__.NiceCar object at 0x112360fd0>\n",
"BMW\n",
"black\n"
]
}
],
"source": [
"# define a class and skip it for now. The class NiceCar has no attributes or methods.\n",
"class NiceCar:\n",
" pass\n",
"\n",
"#instance of a class:\n",
"#a class is a blueprint for creating instances.\n",
"#my_car1and my_car2 are unique instances of class NiceCar\n",
"my_car1 = NiceCar()\n",
"my_car2 = NiceCar()\n",
"\n",
"print(my_car1) #<__main__.NiceCar object at 0x112360208>\n",
"print(my_car2) #<__main__.NiceCar object at 0x112360160>\n",
"\u001b[0;32m<ipython-input-22-aa4b3cd705ea>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mhermes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpaint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"orange\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'hermes' is not defined"
]
}
],
"source": [
"hermes.paint(\"orange\")\n"
"\n"
]
},
{
...
...
@@ -246,7 +456,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.7.1"
}
},
"nbformat": 4,
...
...
%% Cell type:code id: tags:
``` python
s="trees"
```
%% Cell type:code id: tags:
``` python
type(s)
```
%% Output
str
%% Cell type:code id: tags:
``` python
importnumpyasnp
a=np.zeros((5,5))
```
%% Cell type:code id: tags:
``` python
# Look up how to determine callable
```
%% Output
<function len(obj, /)>
%% Cell type:code id: tags:
``` python
s.capitalize()
```
%% Output
'Trees'
%% Cell type:code id: tags:
``` python
deffunc_name(args,kwargs):
pass
```
%% Cell type:code id: tags:
``` python
#Why class:
#Python is an object-oriented language. This allows us to bundle the properties (data) and behaviors (functions) into individual objects, which make them easy to reuse and build upon.
#data and functions associated with a specific class are called "attributes" and "methods", respectively.
```
%% Cell type:code id: tags:
``` python
# define a class and skip it for now. The class NiceCar has no attributes or methods.
classNiceCar:
pass
#instance of a class:
#a class is a blueprint for creating instances.
#my_car1and my_car2 are unique instances of class NiceCar
my_car1=NiceCar()
my_car2=NiceCar()
print(my_car1)#<__main__.NiceCar object at 0x112360208>
print(my_car2)#<__main__.NiceCar object at 0x112360160>
#instance variables:
my_car1.brand='BMW'
my_car1.year='2025'
my_car1.color='black'
my_car2.brand='Porsche'
my_car2.year='2050'
my_car2.color='red'
print(my_car1.brand)#BMW
print(my_car2.color)#red
```
%% Output
<__main__.NiceCar object at 0x112360f98>
<__main__.NiceCar object at 0x112360fd0>
BMW
black
%% Cell type:code id: tags:
``` python
#class methods
#a class method receives the instance as its first argument named by convention "self".