Recently learning Python, I found it has many new places. I've learned some C + + before and found that Python is very similar to C + + in some places. Many of the python concepts are similar in C + +.
There are several currently encountered:
1. "Everything is the object"
In Python, a = ten, B = 10, then a, a, or a, with reference to the same object 10, the "10" is stored somewhere in memory (you can see the address by ID (10))
In C + +, A = ten, B = 10, 10 is an rvalue, no address (? )。
In addition, in Python, function is also an object, function object + Instance Object = Method object, and each component of the program is treated as an object with its own properties and methods
2. Class
1. Python class method The first argument is self, similar to the C + + 's This pointer, which is used to refer to the object calling method.
2. Classes have class variable and instance variable two kinds of data attribute, the former is similar to C + + static data member, belongs to the class, is shared by all, the latter belongs to a certain instance, can only be this A instance visit;
What surprises me most is that the instance instance varible of the same class can vary in number. You can add a property to a instance at run time, or del off . In C + +, the number of data member for a class is fixed and uniform.
3. Classes can also be defined within a class, as in C + +
3. Assigning values
Python supports simultaneous assignment of multiple variables:
A, B = B, a # will be exchanged
4. Functions
The most obvious difference is that functions within a function can be defined in Python, which is forbidden in C + +;
5. Derived Formula
Python has list derivation, dictionary derivation, and so on. In grammar they are closer to English, and in practice they can simplify code and improve readability.
(not finished)
6. Iteration
Python does not have C + + style iterations (that is, to iterate through the sequence with changes in the index variable), only iterations like For_each:
for in range (1): # Python Iteration ------for (int i = 0; i < num; ++i) # C + +-style iterative ------
7. Generators and Lazy Computing
In Python, the yield statement is used to produce the generator to return an iterator. The advantage is that lazy computing can be achieved, that is, the program calculates the results only when needed, without having to calculate the results in advance to save them, thus reducing space consumption. The trick of the generator is that each time the yield statement is executed, the result is returned, and the state of the variable and statement execution is recorded. The next run will start with the statement after the last yield statement.
(not finished)
Python First Impressions