Different ways to get Context in Android

Context is one of the important and most used property. You need Context to perform a lot of things on Android. Be is displaying a toast or Accessing database, you use context a lot while building…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Mutable VS Immutable Objects in Python

To start from the very beginning we need to understand what is object-oriented programming?

Object-oriented programming (OOP)is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior.

OOP focuses on the objects that developers want to manipulate rather than the logic required to manipulate them. This approach to programming is well-suited for programs that are large, complex and actively updated or maintained. This includes programs for manufacturing and design, as well as mobile applications; for example, OOP can be used for manufacturing system simulation software.

That is based on 4 principal pillars which are abstraction, encapsulation, inheritance, and polymorphism. That helps us to abstract objects from the real world.

Classes are user-defined data types that act as the blueprint for individual objects, attributes and methods.

Methods are functions that are defined inside a class that describe the behaviors of an object. Each method contained in class definitions starts with a reference to an instance object.

Attributes are defined in the class template and represent the state of an object. Objects will have data stored in the attributes field. Class attributes belong to the class itself.

Objects are instances of a class created with specifically defined data. Objects can correspond to real-world objects or an abstract entity. When class is defined initially, the description is the only object that is defined.

Mutable objects : are objects that could be changed like “ list, set, dict”

Mutable exemple

Immutable objects: are unchangeable objects like “int, float, bool, str, tuple, unicode”

Immutable exemple

Id function : with id( ) we can get the variable identifier (which is the memory address in the CPython implementation).

Type function: Type( ) it’s the function we use to print the type of an object.

function type()

Like all, there are exceptions in the immutability in python too. Not all immutable objects are really mutable. This will lead to a lot of doubts in your mind. Let us just take an example to understand this.

Now, if we consider tuple tuple = (‘School’,[4,3,1,2]) ;

We see that the tuple has elements of different data types. The first element here is a string which as we all know is immutable in nature. The second element is a list which we all know is mutable. Now, we all know that the tuple itself is an immutable data type. It cannot change its contents. But, the list inside it can change its contents. So, the value of the Immutable objects cannot be changed but its constituent objects can. change its value.

Its important to know the difference between mutable and immutable types and how they are treated when passed unto functions .Memory efficiency is highly affected when the proper objects are used.

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.

Now we know the difference between mutable and immutable objects we know that immutable objects can be used to make sure that the object remains constant throughout the program. However the mutable objects can be changed whether expected or not. Changing a mutable data type won’t change its memory address; on the other hand changing an immutable type can produce errors.

It is actually an array of 262 integers (most commonly used). And this structure is basically used to access these integers fast. They get allocated right when you initialize your NSMALLPOSINTS and NSMALLNEGINTS.

In Cpython the intigers in the range (-5 to 256) those values are used, because they are most used integers. This is a structure to access faster to those numbers, so there is no need to create them.

Add a comment

Related posts:

Why does Costco have the lowest price for gas?

From my personal experience — Every time I need to fill gas, I think of Costco and plan my itinerary around it. I am amazed Costco has shaped the decision-making and plans. All the Costco warehouses…

Sacred Baptism

Sacred is an anagram for scared. Many of the things I consider sacred now were once a cause for fear. I was not a cradle Episcopalian, swaddled in grace through infant baptism, but asked instead to…

Bibit Nangka Thailand Tanaman Buah Sribajong Nh Majalengka Terunggul

bibit nangka thailand tanaman buah sribajong nh HELLOW GAN SILAHKAN DI BACA DULU BERIKUT INI KELEBIHAN DAN KEUNTUNGAN JIKA SIS BELANJA DI LAPAK KAMI: %*. LANGSUNG DARI SUPLIER. {BUKAN DROPSHIPER…