Python 3 - Everything is object

Introduction

Python is a programming language widely employed for website and software development. It is known for its simplicity, object-oriented nature, interpreted execution, and cross-platform compatibility. Its extensive standard library and support for various programming paradigms make it a powerful tool for developers. This article delves into essential concepts such as id and type, distinguishing between mutable and immutable objects, and the significance of Python's treatment of these types in programming. It also explores how arguments are passed to functions, highlighting the implications for object-oriented programming.


ID ()

An object in Python has its unique Id(). Id() is assigned to an object when created and is an object's memory address. It returns a unique identification value of an object stored in the memory.


Type( )

The `type()` function in Python retrieves an object's type, providing insight into its class or data structure. When invoked with a single argument, `type()` returns the object's class type, aligning with its definition within the codebase. Additionally, it can be applied to class instance variables to ascertain their respective types and facilitate dynamic runtime checks within the program.


Mutable objects

According to my understanding, Mutable objects in Python are those that can be changed after they are created. This means that you can modify their values, add or remove elements, and change their internal state without creating a new object. Mutable objects in Python include lists, dictionaries, lists and sets. For instance,

natural numbers = [1, 2, 3, ]

natural numbers .append(4) # Modifying the list by adding an element

print(natural numbers) #output is [1,2,3,4]


Immutable objects

Immutable objects in Python cannot be changed once they are created. After initialising, their values cannot be modified, added, or removed. Examples of immutable objects include integers, floats, strings, and tuples. However, immutable objects can be concatenated. For instance;

mine="Hello"

mine+="my friend" # mine is modified by concatenation.

Print (mine) #output is Hello my friend


Different ways Python treats mutable and immutable objects

It matters how Python treats mutable and immutable objects because it affects how we work with data. Mutable objects can be changed after they are created, while immutable objects cannot. This difference means we must be careful when modifying mutable objects to avoid unintended changes, while immutable objects provide more predictability in our code. Understanding this distinction helps us write safer and more reliable programs.


Arguments passed to functions and their implications for mutable and immutable objects.


When arguments are passed to functions in Python, they can be either mutable or immutable objects. Suppose a function modifies a mutable object passed as an argument. In that case, the changes will affect the original object outside the function, while changes made to immutable objects inside the function won't affect the original object.

An example: Certainly!

```Python

# Function to modify a natural number

def modify_number(my_number):

my_number += 1 # This creates a new integer object and reassigns it to my_number

# Example usage

original_number = 5

modify_number(original_number)

print("Original Number:", original_number) # Output: 5

```

In this example, `modify_number()` attempts to modify the original natural number by incrementing it. However, integers are immutable in Python, so `modify_number()` creates a new integer object instead of modifying the original number. When we print `original_number` after calling `modify_number()`, we see that it remains the same, demonstrating how immutable objects behave differently when passed as arguments to functions.

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics