Everything is an object in Python!

Everything is an object in Python!

Python is an object-oriented programming language, and in Python everything is an object, an?object?is an entity that contains data along with associated metadata and/or functionality.

Every object has these three attributes:

  1. Identity – This refers to the address that the object refers to in the computer’s memory.
  2. Type – This refers to the kind of object that is created. For example- integer, list, string etc.?
  3. Value – This refers to the value stored by the object. For example – List=[1,2,3] would hold the numbers 1,2 and 3

In order to know the address of the objects we have to use the function id().

The?id()?function returns a unique id for the specified object.

All objects in Python has its own unique id.

The id is assigned to the object when it is created.

The id is the object's memory address, and will be different for each time you run the program. (except for some object that has a constant unique id, like integers from -5 to 256)

Example:

print("Id of 10 is: ", id(10)
print("Id of 10.5 is: ", id(10.5)))
------------------------------------
output :

Id of 10 is: 879111306
Id of 10.5 is: 35217761        

If you need to know the type of a specific variable use?type()?function returns the type of the specified object.

Example:

str = 'Python
numbers = [1,2,3,4]
numbers_dict = {'one':1,'two':2,'three':3}

print(type(nums))
print(type(lang))
print(type(nums_dict))'
----------------------------------------------------
output:

<class 'str'> <br>
<class 'list'><br>
<class 'dict'>
        

Object mutability is one of the characteristics that makes Python a dynamically typed language. Though Mutable and Immutable in?Python?is a very basic concept, it can at times be a little confusing due to the intransitive nature of immutability.

What is Mutable?

Mutable is when something is changeable or has the ability to change. In Python, ‘mutable’ is the ability of objects to change their values. These are often the objects that store a collection of data.

For example:

  • Lists
  • Sets
  • Dictionaries.

No hay texto alternativo para esta imagen

As we can see on the example code, once mylist is created has an initial address number, after we change the value of the list by adding one more item("black") on mylist this address number is the same, so by this exercise we can see that we can change the value but the memory address has didn't change, This confirms that we did not create a new object, rather, the same object was changed or mutated.

What is Immutable?

Immutable is the when no change is possible over time. In Python, if the value of an object cannot be changed over time, then it is known as immutable. Once created, the value of these objects is permanent.

For example:

  • Numbers (Integer, Rational, Float, Decimal, Complex & Booleans)
  • Strings
  • Tuples
  • Frozen Sets

No hay texto alternativo para esta imagen

As we can see on the example of the previous code we can not change the value of the tuple once it's created , in the case that we want to add , pop or even change an item of the tuple we have to create a new one in order to do it, and it will be the same with the other data type mentioned previously, we can not modify the value again.

ARGUMENTS PASSED BY VALUE.

Lest's see an example :

we have a function with two arguments

def function(a, b):
     a?=?"hello3"
     b[0]?=?10

a is a ,string with value "hello3"
b is a list, with value 10
This function just assigns value to a couple variables        

Now we call the function with other variables

str?=?"how?are?ya?13" #immutable
list?=?[5]            #mutable


function(str , list)
print(int)
print(list)

---------------------------

output:
how?are?ya?13
[10]
        

What happened here? Why the value of the str is the same?

couple concepts you need to understand before we continue:

By definition,?pass by value?means you are making a copy in memory of the actual parameter's value that is passed in, a copy of the contents of the actual parameter. Use pass by value when when you are only "using" the parameter for some computation, not changing it for the client program.

In?pass by reference?(also called pass by address), a copy of the address of the actual parameter is stored. Use pass by reference when you are changing the parameter passed in by the client program.

Well string as we mention before they are inmutables (Immutable types are passed by value, then the function accesses a copy and not the original value), that's what we can not change the value onces the string is create it, but in the other hand list has a new value again because is a mutable object (Mutable types are passed by reference).

Thanks to read me , I hope this information were helpful for you !









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

Laura Callejas的更多文章

  • What does STEM mean?

    What does STEM mean?

    STEM stands for science, technology, engineering, and mathematics, and it has become the foundation of both education…

  • Web stack incident report

    Web stack incident report

    In this blog, we are going to talk about software system failure, we are going to check the possible reasons for it…

  • What happens when you type google.com in your browser and press Enter?

    What happens when you type google.com in your browser and press Enter?

    In the following article we are going to answer What happens when you type google.com and press enter? so we are going…

  • IoT (The Internet of things)

    IoT (The Internet of things)

    WHAT IS IOT ? Well the Internet of things is a system of connected devices , computers and multiples digital machines…

  • What is recursion ?

    What is recursion ?

    Is the process or concept in programming where the function calls itself, we use recursion cause for certain problems…

  • Static libraries vs Dynamic libraries.

    Static libraries vs Dynamic libraries.

    Libraries are a collection of files that contains functions in this case with extension .c for example stdlib.

  • Static Libraries in C .

    Static Libraries in C .

    Static library is a set of routines, external functions and variables which are resolved in a caller at compile time…

  • What is GCC ?

    What is GCC ?

    It is a compiler for GNU considered standard for operating system system derived from unix.The acronym GCC stands for…

社区洞察

其他会员也浏览了