Python is an object oriented programming (OOP) language, we got that, but what does this really mean? Let's build up an answer slowly but clearly...
Programming is mainly about writing the code of a function that will execute some steps using the parameters that we pass to the function as arguments. These arguments can be from different types: we have strings, ints, floats, lists, tuples, dictionaries...These are Python's built-in types. Each type has its own built-in functions called methods.
Methods allow us to make changes to the values assigned to the variables that we created and stored in the computer memory.
Well that sounded confusing but once we understand this we can move on to the next stage:
We can create our own types, these are called classes. When we initialize a class it creates an object instance of that class (it's like a factory to create instances of an abstract-data-type). Later on, we can define special attributes and methods to this class. For example, we can create a class Point with attributes x_coord and y_coord to enter its coordinates in a cartesian plane. We can also define a function position_in_plane() that returns a string with the coordinates of an object instance of Point. This function will act as a method for class Point.
We have seen how classes work and why Python is called an OOP language. Conceptual abstractions can be made by creating classes that allow us to pretend that we are manipulating the attributes and the methods of real world objects.
No comments:
Post a Comment