Making friends with Objects!
Frank Blation

Making friends with Objects!

An Intro to the intros

Learning python can be overwhelming to say the least, most of us over think and grind our teeth trying to figure out just the basics, and hardly ever understand the questions being asked of us. What if I said there was an easier way? I'd be lying to you because understanding this stuff does take time and effort so let me give you a bit of knowledge that I've gained here at Holberton, so maybe you don't have to work as hard as I did to learn this.

I'm going to go over a few things we've learned this week, feel free to reach out correct and talk to me, if I do a good job, well, maybe I'll keep writing so you can keep learning.

lets get started.

  • id and type
  • mutable objects
  • immutable objects
  • why does it matter and how differently does Python treat mutable and immutable objects
  • how arguments are passed to functions and what does that imply for mutable and immutable objects
  • Memory schema

ID and Type

The id() method returns a unique integer (identity) of a passed argument object. The identifier is an integer, which represents the memory address of the object. The id() function is commonly used to check if two variables or objects refer to the same memory location.

Do you remember pointers in C they did everything Now python does the heavy lifting and you only need Id(). The id() function turns the object pointer into a python integer.

Syntax of an Id() function in Python looks like this:

id(object)        

Id() used in a Python function it may look a little like this:

var = 28
print(var)
print(id(var))         

The output of this code:

>>> 28
    9756576        

let's say you wanted to find out what the object actually was, we could throw in

Type() with id() and it will return what your object is along with where your object lives.

Type() tells you what kind of object something is in Python. It's like checking if something is a car, a bicycle, or a book.

  • Think of it as looking at the label on a package to see what's inside. For example, if you have a box, you might check its label to see if it contains clothes or toys.
  • In Python, everything is an object, and Type() helps you figure out what type of object it is, whether it's a number, a string, a list, a function, or something else.

var = 28
print(var)
print(id(var))
print("Return type of id() : ", type(id(var)))        

The output of this code:

>>> 28
    9756800
    Return type of id() : <class 'int'>        

Here, a variable var was created and a value of 28 was stored. We can see that the id()function returned the variable var's unique id and we can see that the type() returned a class int object, which implies that the value returned by the id() function is an integer.

Mutable Objects

An object is considered mutable if its state or value can be modified after it is created. This means that you can alter its internal data or attributes without creating a new object. Examples of mutable objects in Python include lists, dictionaries, sets, and byte array.

Immutable Objects

In on the other side of the spectrum to mutable objects, we have immutable objects, immutable objects are those whose state cannot be modified once they are created. Examples of immutable objects in Python include number (int, float, complex), string, tuple, frozen set, bytes

There are perks and disadvantages to both immutable and mutable objects this is what I've learned so far.

Advantages of mutable objects         

  • They can be modified in place, which can be more efficient than recreating an immutable object. 
  • They can be used for more complex and dynamic data structures, like lists and dictionaries. 

Disadvantages of mutable objects         

  • They can be modified by another thread, which can lead to race conditions and other concurrency issues. 
  • They can’t be used as keys in a dictionary or elements in a set. 
  • They can be more difficult to reason about and debug because their state can change unexpectedly.

Advantages of immutable objects         

  • They are safer to use in a multi-threaded environment as they cannot be modified by another thread once created, thus reducing the risk of race conditions. 
  • They can be used as keys in a dictionary because they are hashable and their hash value will not change. 
  • They can be used as elements of a set because they are comparable, and their value will not change. 
  • They are simpler to reason about and debug because their state cannot change unexpectedly. 

Disadvantages of immutable objects        

  • They need to be recreated if their value needs to be changed, which can be less efficient than modifying the state of a mutable object. 
  • They take up more memory if they are used in large numbers, as new objects need to be created instead of modifying the state of existing objects. 

Both mutable and Immutable objects are strong important tools in python that are required to become that top programer you dream of being, but really take the time to understand the differences between them and the correct way to use them.

For example if a mutable object is called by reference in a function, it can change the original variable itself. Hence to avoid this, the original variable needs to be copied to another variable. Immutable objects can be called by reference because its value cannot be changed anyways.

MEMORY SCHEMA

Think of a memory schema like a way to remember and organize information in your brain, just like how you organize things in folders on your computer.

In Python, you can create a memory schema using a dictionary (a data structure that holds key-value pairs). Each key in the dictionary represents a category or topic, and the value associated with each key is a collection of information related to that category.

For example, imagine you want to remember information about a person and a place:

  1. Create an empty memory schema (an empty dictionary).
  2. Add information about the person and the place to your schema by creating entries (key-value pairs) for each of them.
  3. You can use keys like "person" and "place" to represent these categories.
  4. For the person, you might include their name, age, and occupation.
  5. For the place, you might include its name, population, and country.

Now, when you want to recall and use this information, you can look it up in your memory schema using the keys. For example, if you want to know the person's name, you use the "person" key to find that part of the information.

So, a memory schema in Python is like a structured way to remember and access information, just like how you organize your thoughts or files in folders to find what you need easily.

# Create an empty memory schema (dictionary)
memory_schema = {}

# Populate the memory schema with information
memory_schema["person"] = {
    "name": "John",
    "age": 30,
    "occupation": "Engineer"
}

memory_schema["place"] = {
    "name": "New York",
    "population": 8_400_000,
    "country": "USA"
}

# Retrieve and use information from the memory schema
if "person" in memory_schema:
    person_info = memory_schema["person"]
    print(f"Name: {person_info['name']}")
    print(f"Age: {person_info['age']}")
    print(f"Occupation: {person_info['occupation']}")

if "place" in memory_schema:
    place_info = memory_schema["place"]
    print(f"Name: {place_info['name']}")
    print(f"Population: {place_info['population']}")
    print(f"Country: {place_info['country']}")
        

This was just a few quick explanations and examples that I could think of. If there are any questions or comments please feel free to reach out to me I want to get better at creating, teaching, and understanding and I can't do any of that with out the help from others! So don't be shy, say hi.


To view or add a comment, sign in

More articles by Frank Blation

Insights from the community

Others also viewed

Explore topics