3.1 introduction
Note
Click here to download the full example code or to run this example in your browser via Binder
3.1 introduction#
The first thing to understanding in object oriented programming in python
is the concept of type
.
In python everything is an object
and it has a type
.
The type
of an object
tells, what kind of characteristics it has,
or what kind of operations it can perform or can be performed on it.
<class 'int'>
<class 'float'>
<class 'str'>
Even the functions have their types
def print_name(the_name):
print('name is: ', the_name)
print(type(print_name))
<class 'function'>
import math
print(type(math))
<class 'module'>
<class 'list'>
<class 'dict'>
<class 'str'> <class 'str'>
<class 'str'> <class 'int'>
<class 'str'> <class 'float'>
Since insan is an instance of class dict
, therefore we can access all functions (methods)
of dict
class through its instance insan. These methods include pop
or update
etc.
insan.pop('weight')
72.5
print(insan)
{'name': 'ali', 'age': 30}
We can always check the methods and attributes available for the instance of
a class by dir(object)
print(dir(insan))
['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
Any method/attribute that starts with single or double underscore “_” is not
for public use. Considering this, we can use any attribute/method of an insan
which we printed above. For example, we can do insan.update or insan.vlaues or insan.keys
etc. For methods
, we have to call them like insan.values() and for other
attributes, we don’t need to call them.
print(dir(a_name))
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
The attributes of a_name are different as compared to those of insan. Therefore, we can not do insan.startswith() or a_name.values().
Question: What methods/functions can be applied on a_int and a_float objects defined at the start of this lesson? Give examples of five such functions by applying them.
Total running time of the script: ( 0 minutes 0.004 seconds)