Everything is object
Object-oriented programming (OOP) is a computer programming model that organizes software design around data or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior.
OOP focuses on the objects that developers want to manipulate rather than the logic needed to manipulate them.
Classes and objects are the two main aspects of object-oriented programming.
Look at the following illustration to see the difference between class and objects:
class
Fruit
objects
Apple, Banana, Mango.
Id & Type
In python, Id is a function that give us or return the identy of an object is an integer, which is guaranteed to be unique and constant for this object during its lifetime.
Two objects with non-overlapping lifetimes may have the same id() value. In CPython implementation, this is the address of the object in memory. Python cache the id() value of commonly used data types, such as string, integer, tuples etc. So you might find that multiple variables refer to the same object and have same id() value if their values are same.
The Type function either returns the type of the object or returns a new type object based on the arguments passed. The Type function has two different forms:
type(object)
type(name, bases, dict)
Mutable objects:
list, dict, set, byte array
Immutable objects:
int, float, complex, string, tuple, bytes
Mutable Objects
In Python, a mutable object is an object that can be modified after it’s instantiated or in other words, created. List, dict, byte array, and set objects are all mutable objects.
Objects of the list class are mutable. In the example below, we create a list of characters and add another character to the list:
Immutable Objects
Immutable objects are ones that cannot be modified after they are instantiated. Immutable objects include numbers (int, float, complex), tuples, strings, and frozen sets, and bytes. In some cases, an object is considered immutable even if some internally used attributes change, but the object’s state appears unchanging from an external point of view.
For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object. Strings and other concrete objects are typically expressed as immutable objects to improve readability and runtime efficiency in object-oriented programming. Immutable objects are also useful because they are inherently thread-safe. Other benefits are that they are simpler to understand and reason about and offer higher security than mutable objects.
Matters of Mutability
The mutability of an object helps inform its functionality. Because mutable objects can change state over time, they can be used to build modular programs where a single object evolves based on other things such as separate program components or user input. On the other hand, immutable objects are important for things like security. For example, strings can be used to store sensitive information such as someone’s password and the fact that they are immutable safeguards against accidental or intentional modification.
How arguments are passed to functions
Call by Value
The most common strategy is the call-by-value evaluation, sometimes also called pass-by-value. This strategy is used in C and C++, for example. In call-by-value, the argument expression is evaluated, and the result of this evaluation is bound to the corresponding variable in the function. So, if the expression is a variable, a local copy of its value will be used, i.e. the variable in the caller’s scope will be unchanged when the function returns.
Call by Reference
In call-by-reference evaluation, which is also known as pass-by-reference, a function gets an implicit reference to the argument, rather than a copy of its value. As a consequence, the function can modify the argument, i.e. the value of the variable in the caller’s scope can be changed. The advantage of call-by-reference consists in the advantage of greater time- and space-efficiency, because arguments do not need to be copied. On the other hand this harbours the disadvantage that variables can be “accidentally” changed in a function call. So special care has to be taken to “protect” the values, which shouldn’t be changed.
Many programming language support call-by-reference, like C or C++, but Perl uses it as default.