Wednesday, January 15, 2020

Object Oriented Programming

Object Oriented Programming


What is Object Oriented Programming?

Object Oriented Programming or OPP for short is a programming paradigm based on the concept of objects that contain data in the form of fields. The concept utilized in python for this type of programming revolves around the concept of encapsulating data inside data and being able to treat a collection of things as one individual thing. Computer programs are designed by making them out of objects that interact with one another. The OOP languages are diverse and the most popular ones are class-based, meaning that objects are instances of classes that also determine their types.

What is id and Types?

All objects contain a unique id where the object is stored in memory, and we can compare different variable ids in order to identify what data type an object is. The "type()" function will tell us what data type an object is.
For example: 


We can also determine if an object is mutable and immutable by its id.


Immutable Objects

Immutable objects are that cannot be changed after they are created. Examples of immutable objects are: integers, floats, bool, string, unicode, tuple.


In this example we assigned a value to "animal" to the string fox and "insect" to the string spider.
What will happen if we tried to changed them? Let's find out!


We can tell it’s a different object because the id’s are different. This means that we have created a new object! By changing the value of the string fox and the string spider we ended up creating 2 new objects. We can't change the value of these types of object but it is possible to assign a new value to it.

Mutable Objects:

Mutable objects are objects who's values can be changed without altering its id in memory.
Examples of Mutable objects are: Lists, sets and dictionaries.


In this example "fox" is mutable object and we set dogs to point to fox in memory and that is why they have the same id. 

How does python handle mutable and immutable objects?

Data types are treated differently whether or not they can change their identity or their id. Mutable objects can change and add their values without altering its id in memory. On the opposite side,  immutable objects can't keep their original id if then change their values. Understanding how these objects work can be helpful on deciding what data type to assign to any object. 

Arguments and Objects

In Python, arguments take objects as parameters for reference. If we pass  a mutable object to a method, we are passing a reference to this object so we can freely modify its value. However,  if we decide to pass an immutable object as a parameter, we can’t change the value of the object. For example:


In this example we are trying to change the content of the list with types of animals by appending another value to it. We can do this because the we are passing a mutable object as an argument.


In this example, we are trying to increment the value of "number". However since we are passing an immutable object and modifying the function without returning the value. The integer is passed but when we try and print it the value stays the same.




1 comment: