All stuff are Objects

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.

No hay texto alternativo para esta imagen

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.

No hay texto alternativo para esta imagen

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:

No hay texto alternativo para esta imagen
No hay texto alternativo para esta imagen

<------- 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

No hay texto alternativo para esta imagen

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.

No hay texto alternativo para esta imagen

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 ).

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

Duvan Rodelo的更多文章

  • Ethical Challenges in AR / VR

    Ethical Challenges in AR / VR

    One of the big areas of ethical concerns surrounding augmented reality and virtual reality is privacy. These devices…

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

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

    When you type an URL such as https://www.holbertonschool.

  • Internet Of Things

    Internet Of Things

    What is the Internet of Things? The Internet of Things, or IoT, refers to the billions of physical devices around the…

  • How recursion works & .... other stuff !

    How recursion works & .... other stuff !

    Recursion. The word alone brings back some painful and confused memory to every CS dev.

  • Making and Using Dynamic Libs (C)

    Making and Using Dynamic Libs (C)

    In order to explain the significance of using dynamic libraries, I’ll need to define some terms. First of all, a…

  • SCRUM Basics

    SCRUM Basics

    Whats a SCRUM ?, basically is team environtment to define objetives and personal contributions to a project…

  • What happens when you type ′ls -l*.c′ on your shell

    What happens when you type ′ls -l*.c′ on your shell

    What is a shell? First, let us define what is a shell. By Marrian-Webster (MW) a shell is: A hard rigid usually largely…

  • Static Libraries in C

    Static Libraries in C

    What are C libraries? One of the tools that compilers supply C programmers. A library file contains a collection of…

  • FANTASTIC 4 TO COMPILE IN C.

    FANTASTIC 4 TO COMPILE IN C.

    About the compiling language we have C. And you also know how to write a C program.

社区洞察

其他会员也浏览了