What's the best way to read Python code?

without an explicit (type) declaration. I'm struggling to figure out how things work - are there any good rule / hint previews you might have for reading python code better? Thank!

+3


source to share


4 answers


Despite the first impression that this question gives, I think it is really really sensible, because it shows that you are subconsciously something that should be of interest to any Python developer, but which I find very neglected in general and in explanations in particular, if not understood.

I mean IMO the Python base is amazingly quirky and smart: this is the data model it was conceived of.
There are no variables in this Python data model in the sense of "chunks of memory whose contents can change," unlike other languages, and in the sense that we do not manipulate these exact variables in Python.

More precisely, this is all an object in Python, and each object is named and designed with an identifier, but neither the object nor the identifier are "variables" in that sense.

This does not mean that in other languages ​​there are no so-called variables, so-called variables, temporarily taking on values ​​that go in and out of them, in implementation hyphens.

...

Let's say the object is constructed with an identifier XYA2

.
Personally, I use this kind of letters to denote any identifier. An identifier is nothing more than a word written in code. This is what appears in the code.
Note that this appearance of letters is the one used by this stackoverflow.com site to represent the sample code within the text by clicking on the {} button. It's easy to recall.

Now, an object whose name XYA2

is the real thing is a specific set of bits > lying in the computer's memory to represent the desired conceptual meaning that it stands for. This set is defined in the C language in which Python is implemented. Personnaly, I highlight letters when I want to assign an object. Then the name object , for me, is called XYA2


XYA2

Identifier XYA2


It is associated with a primary and inaccessible pointer pointing to an object.
This link is done using a symbol table. You will see very few references or allusions to the symbol table at all, here on stackoverflow or elsewhere. However, this is very important, I think.

The pointer associated with the identifier XYA2

points to the XYA2 object
Thus, it is XYA2

directly associated with the pointer and indirectly associated with the object.
Instead of saying "indirectly related", we say "assigned". The object and its identifier are mutually assigned to each other, but the environment of that reference is the primary pointer.

...

And now, something important.
Strictly speaking, a variable is "a piece of memory whose contents can change."
I personally make an effort to never use the word "variable" in any other sense that it is. The problem is that due to the use of the word "variable" in mathematics, this word is very often used indiscriminately and thrown in all wind directions by many developers (not all), even if it is not justified.
Thus, it is almost always used to denote names, such as code identifiers. But this practice is terribly confusing. It should be avoided.

However, an object in Python is not only an instance of a class, it is primarily a specific set of bits; set, which, as far as I know, is NOT a variable in the sense of "a piece of memory whose contents can change."
Hence my opinion is that there are no variables in Python, since the only objects that we can access and manipulate are IDs and objects.

However, the processes under the hood in a Python executable program use a number of pointers, which to my knowledge are real variables in the strict sense of the word.
So, in a sense, you could say that my statement "There are no variables in Python" is false.
This is a matter of point of view.
As a Python developer, conceptually I do not manipulate variables. When I think about an algorithm, I don't think at the level of pointers, even though I know they exist and it is very important to know that they exist. Not at the variable level, but at the Python data model level, I don't understand why I have to agree to believe that there are variables in a Python program. There are variables on the machine and Python is a very high level language.



...

Why did I write all this?

1)
because the nature of the Python data model has a number of implications that cannot be understood if the data model is unknown. Among these consequences, some are interesting because they provide incredible opportunities, others are pitfalls (a well-known example: changing an item in a copied list also changes an item in the original list). This is why it is of the utmost importance to study this data model.

To do this, I recommend that you read these parts of the documentation:
3.1 of objects-values-and-types
4.1 naming-and-binding

...

2)
To justify your answer to your bewilderment: do not fight for what is happening under the hood:
there is a garbage collector, a control meter, carriages of basic vocabulary-like entities, a thunderous ballet of values ​​in the secret of underlying indicators, a lot of checks made by the interpreter ... When something doesn't fit well, a warning is given in the form of exception messages.

Python manages all machines

The only thing you need is to think about the algorithm you want to achieve, and knowledge of the data model is essential for that.

Welcome to the Python universe


Attention!

I don't consider myself a very experienced Python developer, I'm just a hobbyist who had a lot of problems before understanding some essential things about Python.
All of the above description are my personal views on the Python data model. If any point is incorrect in this description, I will be glad to know more about it if the training is conducted with a detailed reasoning.
But I stress the fact that this vision allows me to understand and answer many complex problems and achieve some of the complex mechanisms that Python is capable of. So all of this cannot be false in this above description.

+2


source


You should have a look at the PEP8 documentation This describes Python formatting and style.



+2


source


Read Duck Seal. One of the goals of Duck Typing is that you don't have to think too much about the type of something. What really matters to you is that the variable can be used the way you want.

In Python, you don't need a type declaration, because the name you assign is just a pointer to an object, and furthermore, it can change at any time.

a = None
a = 1+5
a = my_function() # calls my function and assigns the return object to a
a = my_function # Assigns the function itself to a. You could actually pass it as a parameter
a = MyClass() # Runs the __init__() function of the class and assigns the return value to a
a = MyClass # Assigns the class itself to a.

      

These are all valid Python. You can run this sequentially, although the type change is incredulous unless it is completely clear why.

+1


source


if you know c++11

then this is a similer for auto type.

The variable type is decided on the bases of its assignment. 

      

0


source







All Articles