Object-Oriented Programming in Python
Alex Yarosh
Content Quality Analyst @ DataCamp
$$\Large{\text{Object = state + behavior}}$$
$$\text{\textbf{Encapsulation} - bundling data with code operating on it}$$
type()
to find the classimport numpy as np
a = np.array([1,2,3,4])
print(type(a))
numpy.ndarray
Object | Class |
---|---|
5 |
int |
"Hello" |
str |
pd.DataFrame() |
DataFrame |
np.mean |
function |
... | ... |
import numpy as np a = np.array([1,2,3,4])
# shape attribute a.shape
(4,)
obj.
to access attributes and methodsimport numpy as np a = np.array([1,2,3,4])
# reshape method a.reshape(2,2)
array([[1, 2],
[3, 4]])
attribute ↔ variables ↔ obj.my_attribute
,
method ↔ function() ↔ obj.my_method()
.
import numpy as np
a = np.array([1,2,3,4])
dir(a) # <--- list all attributes and methods
['T',
'__abs__',
...
'trace',
'transpose',
'var',
'view']
Object-Oriented Programming in Python