Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How did python implement Type free variables from a statically typed language [duplicate]

Tags:

python

c

I know most of python is implemented in C. I was wondering that how does python work under the hood(in terms of its implementation in C) when it comes to determining what type of variable it is in this case lets say x = 5 now if the check the type of x it will say class int but how is that implemented in C? What checks are made under the hood to determine that it belongs to Class int

like image 817
Ankush Dua Avatar asked Jun 30 '20 01:06

Ankush Dua


People also ask

What is the difference between statically typed and variable-type languages?

As a result, variable types are allowed to change throughout the application. It may sound like an advantage, but it can lead to strange and hard-to-track errors as the code base gets larger. On the other hand, statically typed languages perform type checks upon compilation (think C or Java ).

What's the deal with dynamically typed languages in Python?

Today we’ll explore how to make Python as statically typed as possible. So, what’s the deal with dynamically typed languages? In a nutshell, it means there’s no code compilation, so the Python interpreter performs type checking as code runs. As a result, variable types are allowed to change throughout the application.

Is Python static or dynamically typed?

Still, static typing has some benefits. Today we’ll explore how to make Python as statically typed as possible. So, what’s the deal with dynamically typed languages? In a nutshell, it means there’s no code compilation, so the Python interpreter performs type checking as code runs.

Will Python ever be a static language?

Just a quick disclaimer before we start — Python will always be a dynamically typed language. There’s nothing you can do to make it static as Java or C. PEP 484 introduced type hints, so we’ll have to work with that. The article is structured as follows: Type hints are just that — hints.


Video Answer


2 Answers

Python is in no way "type free" (see "Is Python strongly typed?"). All Python objects have a specific type that determines many of their properties and behaviors. For those that are represented as literals, that type is determined by their lexical form. For other objects, it is determined by how they were computed.

What Python does not have is type declarations or any other built-in form of limiting or predetermining the type of objects that a variable or aggregate may contain. Thus it does not make sense to talk about the type of a variable, such as your x, but only about the type of the value that it contains. And that is what the type() function (for example) computes. So when you ask

how does python work under the hood(in terms of its implementation in C) when it comes to determining what type of variable it is

the answer is in one sense simple: Python doesn't do that at all. Variables don't have types -- only their values do. When you call, say, type(x), you are not determining the type of the variable, but rather the type of its current value.

And Python supports that by ensuring that every Python object, such as one represented by the literal 5, contains rather a lot of data. Significantly, it contains information about the object's value and type, but it also contains various other details as well, such as a reference count and much more. The details are much too broad for an SO answer, but you can find a lot of them in the Python / C API Reference Manual. For the deepest and most intimate secrets, you would want to study the cpython headers and source.

like image 165
John Bollinger Avatar answered Oct 01 '22 02:10

John Bollinger


This is a huge subject.

The below document will give you more understanding.

https://intopythoncom.files.wordpress.com/2017/04/internalsofcpython3-6-1.pdf

Like you said a simple example for integer types

To hold an integer type object, there is structure defined in C as said below

typedef struct {
PyObject_HEAD
long ob_ival;
} PyIntObject;

The object of C structure PyIntObject (section 7.5 of above said document) holds the objects which are integer type.

If you are more interested, setup the environment and debug as said in same section 7.5 of the above document.

Objects/intobject.c and place a debug point on line number 89. Start debugging the application.

PyTypeObject is at higher level for the types to be represented. (look section 7.3 of above said document)

As a programmer, it is curious aspect to know the internals. But do not spend too much time to understand unless you work at interpreter level.

like image 45
Pavan Chandaka Avatar answered Oct 01 '22 04:10

Pavan Chandaka