1. Understand the Background of the New Language
ABC was a teaching language designed specifically for non-professional programmers, but because it was closed, it did not succeed. During Christmas 1989, Guido van Rossum developed a new script interpreter and named it Python, as a kind of successor to the ABC language. The new script interpreter was open, and perfectly combined the usage habits of Unix Shell and C.
2. Understand the Scenarios Where the New Language Is Used
- Website backends
There are plenty of mature frameworks, such as Django, Flask, Tornado - Web crawlers
Python has many libraries available for writing crawlers, such as Scrapy, Beautiful Soup - Scientific computing
Can replace R and Matlab, such as NumPy, SciPy, Matplotlib, Pandas - Data mining, machine learning, big data
Scikit-learn, Libsvm, TensorFlow - System deployment, operations scripts
Shell is suitable for simple system administration work, but complex automation tasks still require Python.
3. Understand the Characteristics of the New Language
Python’s design philosophy is: elegant, explicit, simple. Python’s development philosophy is: there should be one — and preferably only one — obvious way to do it, somewhat similar to Unix. Very unlike other languages, Python uses indentation to determine statement blocks.
Python is an object-oriented, dynamic, interpreted language with excellent modularity. At the same time, Python has a large number of third-party open-source packages that can be used directly, greatly improving development efficiency. Code written in Python is highly readable, and is particularly suited to large multi-person projects. However, Python’s execution efficiency cannot match that of compiled languages such as C/C++.
4. Learn the Data Structures of the New Language
Python’s built-in types include the integer int, the float float, the boolean bool, the string str, the list list, the tuple tuple, the dictionary dict, and the set set. Unlike C, Python does not rely on keywords to define the various types. Python is a dynamically and strongly typed language: it automatically chooses the appropriate data type at runtime. Complex data types such as strings, lists, tuples, sets, and dictionaries are not built-in types in C.
5. Learn the Logical Structures of the New Language
if conditional structure
| |
for loop structure
| |
while loop structure
| |
Keywords: if; for; while;
6. Learn to Define a Standalone Module
Defining a function:
| |
Defining a class:
| |
In multiple inheritance, new-style classes use breadth-first search, while old-style classes use depth-first search.
Defining a package:
To define a package in Python, you only need to create an __init__.py file inside the folder, for example:
| |
Then in other files, you can use the syntax from mypackage import views to import views from the mypackage package.
7. Build a Small Project
Trying to complete a hands-on practice in a domain is the best way to get started in that domain. When learning a new programming language, the period of fastest growth is while you are using that language to complete a project’s requirements.
Django is an excellent web framework written in Python, widely used for building data-driven websites. Django is a large, all-inclusive development framework: it can be used to develop quickly with basically no additional third-party configuration.
The author’s small project requirement was: use QR code scanning to view an online resume.
Once the project requirements are settled, there comes a period of feeling at a loss, not knowing where to start. If someone who has been through it can offer some guidance, you can start coding quickly. Unfortunately, at the time the author had no such person around. After consulting material online, the author finally settled on Jquery, Foundation, and Django to implement this project.
In fact, before this the author had always been writing C++ programs and had no Python programming background; it was only because the backend of this small project’s chosen tech stack used Django that the author started learning Python.
Besides mastering the tech stack used by the project, completing the project also lets you unlock debugging skills and the ability to solve all kinds of programming problems through search engines.
To complete this small project, you need to step out of your comfort zone, break through your existing tech stack, and ultimately achieve rapid technical growth.
8. Learn to Use Libraries to Get Things Done
Usually, publishers package frequently used, reusable functionality into libraries, and make them available for everyone to use.
Libraries can be divided into two categories:
- One is official libraries, which are usually of higher quality and are maintained and updated;
- The other is third-party libraries, of uneven quality, which you need to vet yourself.
For example, Django provides the from django.views.decorators.http import require_POST decorator to ensure that a view function’s request method is POST. Using this decorator simplifies the conditional checks in a view function, which helps write concise and readable code. Of course, Django also has a large number of third-party apps that can be downloaded and used, directly providing a complete feature.
During project development, using libraries sensibly adds a finishing touch.
9. Learn to Organize Code Structure
Studying how to better organize a project’s code is a management science. The author has also written a related blog post,
A Brief Analysis of Django and Project Directory Structure in Practice, which discusses the directory organization of a Django project.
A good project code structure requires learning from the experience of highly regarded open-source projects, and also requires attention to output — becoming a shared standard, jointly maintained, and generating influence.
10. Learn to Make Code Maintainable
Code maintenance falls into two categories:
- Bug fixes made easy, which requires code with good naming conventions, comments, and log output, and writing as little code as possible to achieve the same functionality
- Extension with new features, usually by creating new branches on top of the existing logic. Such branches can in turn be divided into code-level and constant-level. What we want is to extend new features through constant configuration, while reducing code-level modifications
11. Increase the Code Reuse Rate
As the number of years spent using the new language grows and the number of projects you take part in increases, an obvious problem appears: across different projects, there are some very similar features. You may even copy code from previous projects when developing a new one.
At this point, you should think about how to improve the code reuse rate and how to provide common components.
Code reuse can be divided into:
- Code snippets (Snippets)
Maintaining a Snippets List is a good idea - Common components (Components)
Common components need to be separated out of the project, which takes some effort, but the payoff is large
The process of separating common components from a project requires attention to high cohesion within modules and low coupling between modules. In the process of module separation, we come to understand the application system more deeply. Completing this process in turn greatly improves development efficiency.
More importantly, we do not merely separate a common component out of a project; using common components provided by others also becomes easy.
The next time you develop a project and need a new feature, your first thought may not be to write code, but to search a common component library or Github for a similar package, and then, after a small amount of adaptation, use it in the project.
12. Pay Attention to Performance
For web applications, high concurrency, high availability, and high consistency are eternally unchanging topics. Only when you keep challenging the application system do those hidden, previously ignored problems gradually come to light. In this process, you continually discover the application system’s bottlenecks, resolve them, and repeat, deepening your understanding of the business and of the application system.
A performance report is the result produced by performance testing. A performance report is like a quality inspection report: it is the credential that lets users trust and use the product. High concurrency, high availability, and high consistency are not slogans; they require data as proof and continuous performance optimization.
13. Reading Source Code
Reading source code helps improve your own coding level. Reading the source code of the Python interpreter helps write high-performance code. Reading Django’s source code helps write Django apps that are easy to maintain and easy to update. From the moment you start learning this new language, you can begin trying to read source code.
