Object Oriented Programming - No Objections Here!

Object Oriented Programming - No Objections Here!

If you're new to the world of Python programming, don't worry! I'm here to help you understand some fundamental concepts that form the backbone of Python. In this article, I'll guide you through the world of objects, learn about their types, and understand why they matter in Python. While the best way to learn coding is sitting down at a computer and writing lines of code, it is essential not to overlook key concepts of the Python language. Python operates on the principles of object-oriented programming, which might sound daunting at first. So, let's start by unraveling the basics—what exactly is an object, and what does it mean for Python?


What is an object?

At its core, Python is all about objects. Think of an object as a self-contained package that holds both data and actions. Whether it's a simple number or a complex piece of information, in Python, everything is an object. In Python, objects are created from classes, which are like blueprints for objects. Imagine a class as a template that defines the properties and behaviors an object should have. When you create an object, it's like building something based on that template. For example, a "Car" class could define properties like "make" and "model," and actions like "drive" and "brake."


Aliases and Variable Identity:

An alias happens when two or more variables point to the same object. If you create a list of numbers named 'x':

>>> x = [1, 2, 3]        

And then state that the variable 'y' is equal to 'x'

>>> y = x        

What you are actually doing is assigning 'y' as an alias for 'x'. Basically this means that both 'y' and 'x' are name-tags on the list [1, 2, 3]. If you change your mind and change 'x''s value:

>>> x = [1, 9]        

It will also change the value of the other name-tag, 'y'. We can see this by printing the value of 'y':

>>> print(y)
[1, 9]        

To check if two variables are aliases of each other, you can use the 'is' operator:

>>> x is y
True        

(The line with ">>>" is input from me, and the line "True" is Python responding to that statement.)


Object Identity:

The identity of an object is represented by a numeric value. You can think of the object's identity as its "address" in the computer's memory, allowing the program to locate and interact with the object. You can use the id() function to see if two variables point to the same object. If id(x) and id(y) return the same value, that means they reference the same object. You can use id() to display the memory address of an object. For example, print(id(x)) will show you where the object referenced by x is stored in memory.

>>> x = [1, 2, 3]
>>> print(id(x))
4002984     <-- this is the memory address of the object '[1, 2, 3]'        

When you use variables in Python, you're essentially creating name-tags for objects. These name-tags (actually called 'references') act as labels that allow you to interact with objects without worrying about where they are stored in memory. Assignment, on the other hand, binds a variable to an object. For instance, if you write x = 42, you're assigning the variable x to hold the value 42.


Mutable and Immutable Objects:

Here's where things start to get complicated. In simple terms, mutable objects can be changed, while immutable objects cannot. If you modify a mutable object, any references to that object will reflect the changes because they point to the same object in memory. Examples of mutable objects in Python include lists, dictionaries, sets, and user-defined classes.

>>> x = [1, 2, 3]
>>> x.append(4).    <- I used 'append' to add (4) to my list        

Immutable objects cannot be modified after they are created. Any operation that appears to modify an immutable object actually creates a new object with the modified value, leaving the original object unchanged. Examples of immutable objects in Python include integers, floats, strings, tuples, and frozen sets. In this example:

>>> x = [1, 2, 3]
>>> x.append(4).    <- I used 'append' to add (4) to my list        

'x' got updated to include the number '4' in its list. However, this didn't recreate '1' or '2' et cetera. This is because integers are assigned their own unique memory locations when you first start Python up. In this example, the list 'x' is a mutable object (since we could add 4 to its list), and the integers themselves are immutable objects.

How integers are stored in memory:

Python has a unique way of handling certain integers efficiently. It reserves a range of integers between 'NSMALLPOSINTS' and 'NSMALLNEGINTS' to speed up code execution. I'll give them the nicknames '+INT' and '-INT'. Python uses '+INT' and '-INT' to avoid the overhead of creating new objects for commonly used small integers. When you create variables with small integer values within the range covered by '+INT' and '-INT', Python simply references these preallocated objects instead of creating new ones. This optimization reduces memory consumption and improves Python's overall performance. The values chosen for '+INT' and '-INT' are based on the most frequently used integer values in Python, normally the range (-5 to 256).        

To see what type of object a variable is, you can use the function 'type':

>>> x = [1, 2, 3, 4]
>>> print(type(x))
<class list>        

It kindly responded by telling us it is a list (which we now know is a mutable object).


How Python Passes Variables to Functions:

When you pass a variable to a function, Python uses a mechanism called "call by object reference" or "call by sharing." It means you're passing a reference to the object, and changes inside the function can affect the original object. Here's an example to illustrate this concept:

>>> def modify(list):   <- creating a function that modifies a list
        list.append(4)


>>> list = [1, 2, 3]    <- creating a list
>>> modify(list)        <- calling the function to modify the list


>>> print(list)         <- printing the list after being modified
[1, 2, 3, 4]        

In this example, the 'modify' function takes a reference to the original list 'x'. Inside the function, we add (append) an element to the end of the list . When we print the original list 'x' after calling the function 'modify(list)', we see that it has been modified. This demonstrates the "call by object reference" behavior in Python.


Tuples and Frozen Sets:

A tuple is an ordered collection of elements that is similar to a list, but with one crucial difference: tuples are immutable. This means that once you create a tuple and assign values to it, you cannot change those values. While tuples and frozen sets are generally immutable, they can contain mutable objects. This implies that although you cannot modify the container (the tuple or frozen set), you can still alter the objects contained within (a list). Here's an example that should illustrate this concept:

>>> my_tuple = (1, [2, 3], 4) <- create tuple

>>> my_tuple[0] = 10.         <- Attempt to modify the tuple itself
ERROR     

>>> my_tuple[1][0] = 10       <- modifying the list inside the tuple
>>> print(my_tuple)
(1, [10, 3], 4)        

In this example, we create a tuple 'my_tuple' containing an integer, a list [2, 3], and another integer. We attempt to modify the tuple itself, which is not allowed because tuples are immutable. However, we can modify the list [2, 3] within the tuple, and this change does not affect the tuple itself. This showcases the unique behavior of tuples and frozen sets in Python, where the container remains unchangeable, but the objects inside it can be modified if they are mutable.


Conclusion:

By grasping the fundamental nature of objects, their types, and how they behave in Python, you're not only setting a solid foundation but also gaining the tools to write cleaner, more efficient code. These concepts will help you make informed decisions about object mutability and confidently navigate the world of functions. Python's unique approach to objects and references enhances its versatility and makes it a powerful choice for a wide range of applications. So, dive in, explore, and hopefully you now have no objections to object oriented programming!

要查看或添加评论,请登录

社区洞察

其他会员也浏览了