OOP-sy Daisy, I learned a lot
about Object Oriented Programming
Python is an Object Oriented language, and in the case of Python “everything” is an object. An object is simply abstract container for data. There are many different types of objects, and because users can create objects, there is an endless number of types. These objects allow for easier manipulation and movement of data throughout a program. It is important to understand how python handles the manipulation and creation of objects, in order to properly use them.
id() and type()
Whenever an object is created in Python, it is given an id, which represents where it is stored in memory (in CPython is appears as a hex number vs a long integer in Python). This is useful info to pay attention to. When objects are being created the id permanently tied to it, so you can identify if whether a new variable is pointing to an object that has already been created, or if the variable has been assigned to a new object. Type on the other hand lets you know the class (i.e.: int, tuple, list) of the object. Now if you understand which classes are mutable vs. immutable, this can help you determine how that data will be handles as it is manipulate, copied, and moved. Both of these are functions within Python that can be called on a variable.
Mutable objects
Python objects can be mutable or immutable. Mutable objects can be changed and manipulated. The actual element is changes. For example, lists can be added to, or an index of the list changed and the memory address (checked using id()) stays the same. The object that existed at that address is still there, but the object is inherently changes. As a result if the user calls type on an object and sees that it is a list, dict, or set, then they understand some basic features of that object without even knowing what the object contains, and therefore, can start to understand how they will need to handle the data inside.
Immutable objects
Immutable objects, are fixed. They do not change. Once created, the can be de-referenced but the object remains in memory (unless del is called, and even then some objects can remain). Immutable objects, such as int, float, bool, string, unicode, tuple, are useful when the data needs to be held constant. It is important to know that the variable (or reference) that points to an immutable object can be changed, but if math is done on the tuple that existed a new tuple is created at a new location in memory.
It is important to know the difference in how data is handled in any programming language so that it is handled correctly and safely. Before programming begins decision are made on how to solve the problem. Choosing an immutable type for a program that will be changing the data constantly can lead to massive amounts of data being used needlessly. Knowing the limits of the objects also helps a programmer understand why the output may fail, and how to guard against improper input.
An example of when this knowledge is important is when passing objects around. Python handles passing using “pass by value”. So when a function is called, the valuable of the inputs is grabbed and not the actual variable. As a result, unless you set the function equal to the original reference, the original reference will remain unchanged by the function, even though the function does in fact run.
Data manipulation can already be tricky because we do not see the values change before our eyes. As programmers, we are faced with simple inputs and outputs, so knowing what is happening behind the veil is important to problem solve when the programs fail to function as intended, and to make sure that future programs function efficiently! HOOPfully the more you know about your data and how it works, the less bugs you run into.