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
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.
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.
Recommended by LinkedIn
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
Disadvantages of mutable objects
Advantages of immutable objects
Disadvantages of immutable 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:
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.