All stuff are Objects
The first time when I hear about Pyhton , the message was "Python is dangerous". But in the coding way, like in the life "dont judge a book by its cover", so I decided to introduce myself on the snake world.
Before to start we need to know what is Python?
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
It is used for: web development (server-side), software development, mathematics, system scripting.
Now, we are going talking about OOP(object-oriented-programming), is about to think that everything when you are programming is an object, so an object have some features, datas and methods.
ID() and Type()
An object have a ID , a type and a value. The id() of the object is an integer that is going to identify that object during its lifecycle, in a few words: the object address. Now the type, this determine the kind of operations that the object support, and this bot can be modify.
So , we have a variable(a) = 100, type a (int), id (108997536).
Now, logic indicates that if variables have the same value , actually theyre going to point to the same object. Lets try:
<------- Look this:
The is operator checks if a given object is the same in its ID to another object. The == operator instead checks if object content are the same as the contents of another. Exactly, you can have two objects with exactly the same content but being two completely diferents. Crazy no?.
Then, what's the mistery?
If is create an int from the range of -5 and 256, you are actually referencing to the existing object
#ifndef NSMALLPOSINTS #define NSMALLPOSINTS 257 #endif #ifndef NSMALLNEGINTS #define NSMALLNEGINTS 5 #endif #if NSMALLNEGINTS + NSMALLPOSINTS > 0 /* References to small integers are saved in this array so thatt they can be shared. The integers that are saved are those in the range -NSALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). */ static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; #endif #ifdef COUNT_ALLOCS Py_ssize_t quick_int_allocs; Py_ssize_t quick_neg_int_allocs; #endif
Mutable / Inmutable objects
Having a type string variable, with an integer identifier, if we try to concatenate (string + variable), in fact an object has been created, but if you try to modify directly the string , you will see a typeerrror massage.
(INMUTABLE OBJECTS)
A mutable object can have any number of names and if you change something it will be reflected to the rest of the names assigned to that value, this explains also why when we use the is operator and the == operator both print True.
See that the list have its identifier number, later we add a element to the list and the id is still the same, after that we deleted a element of the list and still have the same id. Always the same object.
(MUTABLE OBJECTS)
If you think that all this is a brainbreaker , in fact my friend youre right!! , and Python still being dangerous, but funny. Hope that you enjoy this little article.
Tsssssssse you (pythonistic bye ).