What are the advanced skills in Python development?

Source: Internet
Author: User
Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply content: Thank you, old man @ original blog invitation :)

I have listed a few tips. I don't know if it is a good skill, but I personally think it is very useful.

1. make good use of the iterator

The iterator is available in many languages, and it is very nice to use the iterator in appropriate scenarios in Python ". First, because the iterator generates an object each time, proper use can effectively save memory; second, it can achieve partial "latency calculation. In addition, because of the existence of Generator (yield keyword) and Generator Expression, sometimes the iterator can be used to improve code readability.

For example, itertools. islice (calculate_for_value (v) for v in values), 0, 12) can only be calculated within the range of [0, 12), and delay is calculated, that is, computing is performed only when the iteration reaches that object. For example, any (I % 3 = 0 for I in numbers) can find the first value in numbers that can be divisible by 3, because it is a Generator Expression (a type of iterator), the any function will return immediately after it is found out, and I % 3 does not need to be calculated for the entire numbers list.

We recommend this library erikrose/more-itertools. It contains a lot of practical iterator functions, which is a good supplement to the standard library itertools.

2. Descriptor)

The Python descriptor is an abstraction of "attributes". after a descriptor is defined as a class attribute, it can control the get, set, and delete actions of the instance with the same name on the class, the magic method has a finer granularity than the instance-level magic method such as _ getattr _ and is easier to reuse. This document provides a brief description of Descriptor HowTo Guide. It can be seen that Python's "instance method" and @ property are all implemented by it, and some third-party libraries are also useful (for example, SQLAlchemy's Column, WTForms Field, and even the enum added by Python 3.4. enum type ).

Using the descriptor feature, you can implement some very convenient customization in the business code. for example, you can implement a cached_property that can cache the returned value (or you can directly use Werkzeug without yourself ). ).

3. try not to use the reflection technique to fight with language

One of my personal points is that it is better to use a programming language to get things done with language. Because closed doors are difficult to create circles, let alone the ability to fight against the language design at the language user level.

The question is "advanced skills". for a dynamic language, reflection is certainly a high-level skill. However, I have seen some Python reflection to scan all the. py files in some packages and then automatically import all modules under the package. Not to mention that this method destroys the settings of Python "module is namespace" and lazy import. There are a bunch of problems from the perspective of "correctness. This practice only takes into account the scenario of the module file system, and does not take into account the possible scenario of the module in a zip. Even if you want to enhance the implementation, consider the zip import, then Python also has the PEP 302 The defined Import Hook usage is completely useless by such a hack. This fight with language approach is hard to achieve true "correctness ".

So I think another Python technique is to think carefully when using "Advanced Skills.Calm down and think about whether you are in fight with language. if so, we recommend that you stop. Otherwise, consider whether Python is really worth improving. The latter requires a lot of consideration, not 10 minutes of thinking. If the answer from the latter is "yes", I think it is possible to write a PEP and discuss it with the community to see if improvements can be directly applied to future Python versions, instead of using an ugly hack implementation in your own code to combat the language itself.

-------------------------------------------------------------------------------

Other Python-specific skills, such as decorator and contextmanager, are not listed because Python developers are familiar with each other.

-------------------------------------------------------------------------------

Supplement: in fact, there is an ultra-long list of Hidden features of Python. ...... However, many of the features in it are now daily. we recommend this post on Stack Overflow:
Hidden features of Python You can check out my blog. this is a record of reading and thinking. I haven't finished writing it yet. it's basically a day or two.

Python advanced features 1: Non-variable types that are easy to ignore
Python advanced features 2: list derivation, generator and iterator
Python advanced features 3: Magic _ call _ and return functions
Python advanced features 4: Functional programming
Python advanced features 5: about python's dynamic attributes
Python advanced features 6: Using _ slots _ can really save a lot of memory
Python advanced features 7: closure and decorator
Python advanced feature 8: Do you really understand classes, objects, instances, and methods?
Python advanced features 9: poor polymorphism
Python advanced features 10: boring @ property
Python advanced feature 11: expand basic data types (dict) It is recommended that you take a look at Python advanced programming.
For example, yield and property are advanced techniques.
There are many details, and it is too hard to list them one by one.
As for list parsing, it is quite basic. I personally think The most important philosophy of the python language is Readability. Therefore, it is the most important to improve the readability of the code. it is also an advanced technique that distinguishes it from its primary skills.


It is easy to accomplish one thing. The difference is the cost and the elegance. I must have used coroutine to write python. I have never used any advanced skills. I just want to talk about some tips in team collaboration:
1. docstring
Writing docstring to modules, classes, and functions is a good habit. let's take a look at the official example:

>>> def my_function():...     """Do nothing, but document it.......     No, really, it doesn't do anything....     """...     pass...>>> print my_function.__doc__Do nothing, but document it.    No, really, it doesn't do anything.
Lambda
For example

calculator{    'plus' : lambda x,y : x + y,    'minus' : lambda x,y : x - y}res = calculator['plus'](2, 3) # res = 5
See Python advanced programming.
And a http://www.kokojia.com/course-526.html. The stuff in this link is quite good. if you are interested, I hope it will help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.