Although Python is based in C, it doesn’t mean its complex and similar to it, in fact, Python is based on simplicity and being as easy as it possibly can and most of all, its categorized as an OOP (Object-Oriented Programming) language. OOP can be seen in a simple way as a bridge “to put real world entities as software objects, which have some data associated with them and can perform certain functions”; with everything being an object, the variables, the classes, anything that holds data is an object.
Id and type
Python has two built-in functions which allows to know the type of an object and its id.
type()
Returns the type of the given object.
id()
Returns the identity of the object; this identity is an integer and unique, meaning that once you initialize a variable its id will not coincide with another object, unless the object contains the same value as the previous one, as seen in the second example.
Mutable & Immutable objects
Mutables objects are those that can be modified after being created. List, dict, set, byte array, are mutable objects, they can change any value at any given position without raising errors.
Immutable objects are tuples, integers, float, strings, bool, their values cannot be modified.
but… what’s the big deal of mutable and immutable?
Well, immutable objects are safe to use since their value won’t changed during the execution time of the program, we can freely pass around objects without the fear of modification but its not the same with mutable objects since their value can be modified, and can be pointing to the same object also called as aliasing that leads to inconsistencies and threatens correctness of the code. One part of code mutating the value and another part of code affected due to this mutation.
Arguments passed to functions
When writing mutable objects we pass them an argument or more than one to output something we want to show, an example would be like the following.
With inmutable objects, it would look something like this
The values inside of the tuple cannot be modified and when doing so it brings an error declining the operation.
To finish, everything is an object in python.
Remember #pythoniscool.