Python is Awesome…
In Python, Everything is object… What is object ?
Object: Everything in Python is an object, and almost everything has attributes and methods. All functions have a __doc__ attribute that returns the documentation string defined in their source code. The sys module is an object that contains (among other things) an attribute called path
What is Class and Methods ?
Method: A method is a function that “belongs to” an object. In Python, the term method is not limited to class instances: other types of objects may also have methods.
Object: Everything in Python is an object, and almost everything has attributes and methods. All functions have a __doc__ attribute that returns the documentation string defined in their source code. The sys module is an object that contains (among other things) an attribute called path.
ID and TYPE:
ID: id() is an inbuilt function in Python. id(object)
As we can see the function accepts a single parameter and is used to return the identity of an object. This identity has to be unique and constant for this object during the lifetime. Two objects with non-overlapping lifetimes may have the same id() value. Knowing that, objects that have a constant unique ids such as integers from -5 to 256 remain the same. You can think of the id as the object’s memory address.
Type: type() is a function in python. Type(object)
Types are ways to classify object data types. Python3 provides built-in data types including int, float, tuple, list, set dict and many others and are referred to as class types. To find the specific class type of a variable or segment of data, use the built-in
Mutable and Immutable objects:
Mutable Objects: Python objects that are mutable are dictionaries, lists, sets, and custom classes. This kind of objects can be changed after being created.
Immutable Objects: These are of in-built types like int, float, bool, string, Unicode, tuple. An immutable object can’t be changed after it is created.
Why Does it matter if Mutable or not ? And how Python treat them ?
Lists and Dictionaries are mutable it means that we can change their content without changing their identity. Other objects like Integers, Floats, Strings and Tuples have no provision to change there assigned value for an index.
An immutable object is used to ensure that it remains constant throughout the program. The values of mutable objects can be changed at any time and place, whether it’s expected or not.
Good to know:
- Mutable objects (dict) can change in value, but the object remains the same.
- Immutable objects will create a new object based on the changed value.
- Mutable objects have easier access, thus making adjustments more useful.
- When you want something to remain unchangeable, you can rely on immutable objects because its value will not change after you modify the program.
How arguments are passed to functions and what does that imply for mutable and immutable objects ?
The authors who call the mechanism call-by-value and those who call it call-by-reference are stretching the definitions until they fit.
Correctly speaking, Python uses a mechanism, which is known as “Call-by-Object”, sometimes also called “Call by Object Reference” or “Call by Sharing”.
If you pass immutable arguments like integers, strings or tuples to a function, the passing acts like call-by-value. The object reference is passed to the function parameters. They can’t be changed within the function, because they can’t be changed at all, i.e. they are immutable. It’s different, if we pass mutable arguments. They are also passed by object reference, but they can be changed in place in the function. If we pass a list to a function, we have to consider two cases: Elements of a list can be changed in place, i.e. the list will be changed even in the caller’s scope. If a new list is assigned to the name, the old list will not be affected, i.e. the list in the caller’s scope will remain untouched.