What does it take to be proficient in Python?

What does it mean to be proficient in Python?

This answer may be a bit long, I will first give my understanding of proficiency in Python, and then give some difficult knowledge points in Python. If you have fully understood the various knowledge points I listed before reading my answer, then I believe you are already proficient in Python. If not, I hope this answer will make you aware of the deficiencies in your Python knowledge, and where to improve in your future studies.

Mastery is a false proposition

How to be proficient in Python is a very interesting question.

Few people will say that they are proficient in Python, because those who dare to say that they are proficient these days will be rubbed on the ground. Second, we really shouldn’t be obsessed with programming languages, but focus on domain knowledge. For example, you can say that you are proficient in database, proficient in distributed, and proficient in machine learning, and that is considered as your strength. However, you say you are proficient in Python, which is not cool at all, and it is not well recognized in the industry.

Furthermore, Python is used in such a wide range that it is impossible for a person to be proficient in all fields with limited energy. Take the Python application field on the Python official website as an example, Python has the following applications:

  • Web Programming: Django, Pyramid, Bottle, Tornado, Flask, web2py
  • GUI Development: wxPython, tkInter, PyGtk, PyGObject, PyQt
  • Scientific and Numeric: SciPy, Pandas, IPython
  • Software Development: Buildbot, Trac, Roundup
  • System Administration: Ansible, Salt, OpenStack

Why recruitment requires proficiency in Python

Most people have a biased understanding of Python, thinking that Python is relatively simple. Compared with C, C ++ and Java, Python is easier to learn, so there are so many engineers who simply understand a little grammar and claim to know Python.

For example, if an engineer wants to interview for a C ++ position, he will at least find a C ++ book and study it carefully before applying for the job. Python is not the case. Many students only spent a little time to understand the syntax of Python and said that they are familiar with Python. This also makes Python interviewers more likely to encounter unqualified job applicants than interviewers in other directions, wasting everyone’s time. In order not to cause trouble for themselves, Python interviewers can only raise their requirements and require job applicants to be proficient in Python.

How to be proficient in Python

Since it is impossible to be proficient in Python itself, and the interviewer requires proficiency in Python, as a job seeker, what level should he reach before he dares to apply? My point is that jobs that require proficiency in Python are full-time Python developers, and Python is their primary language. To be a colleague with them, you need at least:

1. Be able to write Pythonic code (what is Pythonic code, please see my answer under another question:

2. Familiar with some advanced features of Python

3. Have a better understanding of the advantages and disadvantages of Python

This may be more abstract, not easy to understand. Let’s look at a few examples. If you can fully understand each example here, then you can successfully pass the “proficient in Python” job interview.

Since it is impossible and meaningless to be proficient in Python, why is proficiency in Python required in all recruitment requirements? I think it’s all forced. Why do you say that, let me speak slowly.

1. Context Manager

When you are programming, you often encounter such a scenario: first perform some preparation operations, then execute your own business logic, and then perform some cleanup operations after the business logic is completed.

For example, open a file, process the contents of the file, and finally close the file. For another example, when a multi-threaded program needs to access critical resources, the thread first needs to acquire a mutex, and when the execution is completed and is ready to exit the critical section, the mutex needs to be released. For these situations, Python provides the concept of a context manager (Context Manager), which can be used to control the preparatory actions before the execution of the code block and the finishing actions after the execution.

Let’s take processing files as an example to see how this situation is handled in other languages. Java-style/C++-style Python code:

 myfile= open(r'C:\misc\data.txt')
    try:
        for line in myfile:
            ...use line here...
    finally:
        myfile. close()

Pythonic code:

 with open(r'C:\misc\data.txt') as myfile:
        for line in myfile:
            ...use line here...

We are talking about proficiency in Python in this question, obviously, just knowing context managers is not enough, you also need to know:

1. Other usage scenarios of the context manager (such as database cursor, lock)

    • The context manager manages locks
 class FetchUrls(threading. Thread):
            ...
            def run(self):
                ...
                with self.lock: #Use the "with" statement to manage the acquisition and release of locks
                  print 'lock acquired by %s' % self.name
                  print 'lock released by %s' % self.name
    • The context manager manages the database cursor
 import pymysql

        def get_conn(**kwargs):
            return pymysql.connect(host=kwargs.get('host', 'localhost'),
                    port=kwargs. get('port', 3306),
                    user = kwargs. get('user'),
                    passwd = kwargs. get('passwd'))

        def main():
            conn = get_conn(user='laimingxing', passwd='laimingxing')
            with conn as cur:
                cur. execute('show databases')
                print cur. fetchall()

        if __name__ == '__main__':
            main()
    • The context manager controls the precision of operations
 with decimal. localcontext() as ctx:
            ctx.prec = 22
            print(decimal. getcontext(). prec)

2. The context manager can manage multiple resources at the same time

Suppose you need to read the content of a file, process it, and write it to another file. You can write Pythonic code, so you use context managers and are satisfied with code like this:

 with open('data.txt') as source:
            with open('target.txt', 'w') as target:
                target.write(source.read())

You have done a good job, but you must always remember that you are a person who is proficient in Python! People who are proficient in Python should know that the above code can also be written like this:

 with open('data.txt') as source, open('target.txt', 'w') as target:
            target.write(source.read())

3. In your own code, implement the context management protocol

You know that the syntax of the context manager is concise and beautiful, and the code written is not only short, but also highly readable. So, as someone proficient in Python, you should be able to easily implement the context management protocol. In Python, we just have to implement the following two protocols ourselves:

    • __enter__(self) Defines what the context manager should do at the beginning of the block created by the with statement. Note that the return value of __enter__ is bound to the target of the with statement, or the name after the as.
    • __exit__(self, exception_type, exception_value, traceback) Defines what the context manager should do after its block has been executed (or terminates). It can be used to handle exceptions, perform cleanup, or do something always done action immediately after the block. If the block executes successfully, exception_type, exception_value, and traceback will be None. Otherwise, you can choose to handle the exception or let the user handle it; if you want to handle it, make sure __exit__ returns True after all is said and done. If you don’t want the exception to be handled by the context manager, just let it happen.

2. Decorator

Since the title of our question is proficient in Python, I assume that everyone already knows what a decorator is and can write a simple decorator. So, did you know that there are also some precautions for writing decorators.

Let’s look at an example:

 def is_admin(f):
        def wrapper(*args, **kwargs):
            if kwargs.get("username") != 'admin':
                raise Exception("This user is not allowed to get food")
            return f(*args, **kwargs)
        return wrapper


    @is_admin
    def barfoo(username='someone'):
        """Do crazy stuff"""
        pass

    print barfoo.func_doc
    print barfoo.__name__

    none
    wrapper

After we decorate the function with the decorator, the function name and help information of the original function cannot be obtained correctly. In order to obtain this information, we need to use @functool.wraps. As follows:

 import functools
    
    def is_admin(f):
        @functools. wraps(f)
        def wrapper(*args, **kwargs):
            if kwargs.get("username") != 'admin':
                raise Exception("This user is not allowed to get food")
            return f(*arg, **kwargs)
        return wrapper

For another example, we want to obtain the parameters of the decorated function for judgment, as follows:

 import functools
    def check_is_admin(f):
        @functools. wraps(f)
        def wrapper(*args, **kwargs):
            if kwargs.get('username') != 'admin':
                raise Exception("This user is not allowed to get food")
            return f(*args, **kwargs)
        return wrapper
    
    @check_is_admin
    def get_food(username, food='chocolate'):
        return "{0} get food: {1}". format(username, food)
    
    print get_food('admin')

This code does not seem to have any problems, but the execution will go wrong, because username is a positional parameter, not a keyword parameter, we cannot get it through kwargs.get(‘username’) in the decorator The username variable. In order to ensure flexibility, we can modify the code of the decorator through inspect, as follows:

 import inspect
    def check_is_admin(f):
        @functools. wraps(f)
        def wrapper(*args, **kwargs):
            func_args = inspect.getcallargs(f, *args, **kwargs)
            print func_args
            if func_args.get('username') != 'admin':
                raise Exception("This user is not allowed to get food")
            return f(*args, **kwargs)
        return wrapper

There is still a lot of knowledge about decorators, such as how to decorate a class, the usage scenarios of decorators, and the disadvantages of decorators. Do you all know these? Interested students can read my previous blog (

Introduction and improvement of python decorators

3. Global variables

Regarding Python’s global variables, let’s start with a question: Does Python have global variables? Maybe you were confused when you saw this question, it doesn’t matter, let me explain.

From Python’s own point of view, Python has global variables, so Python provides us with the global keyword, and we can modify global variables in functions. However, from the perspective of C/C++/Java programmers, Python has no global variables. Because, Python’s global variables are not at the program level (that is, globally unique), but at the module level. A module is a Python file and is an independent, top-level namespace. The variables defined in the module belong to the namespace. Python does not have real global variables, and the variables must belong to a certain module.

Let’s look at an example to fully understand the above concepts. Three different ways of modifying global variables:

 import sys
    
    import test
    
    a = 1
    
    def func1():
        global
        a + = 1
    
    def func2():
        test.a += 1
    
    def func3():
        module = sys.modules['test']
        module.a += 1
    
    func1()
    func2()
    func3()

Although this code seems to be operating on global variables, in fact, it also involves the working principle of namespaces and modules. If you don’t know what happened clearly, you may need to supplement your knowledge.

4. Time complexity

We all know that in Python, a list is a collection of heterogeneous elements, and can grow or shrink dynamically, and can be accessed through indexing and slicing. So, how many people know that list is an array rather than a linked list.

I think everyone knows the knowledge about arrays and linked lists, so I won’t repeat them here. If we don’t even know the time complexity of our most commonly used data structures in the process of writing code, how can we write efficient code? If we can’t write efficient code, how can we claim to be proficient in this programming language?

Since list is an array, what data structure should we use when we want to use a linked list? When writing Python code, if you need a linked list, you should use deque in the standard library collections, deque is a doubly linked list. There is a queue in the standard library, which looks a bit like deque. What is the relationship between them? This question is left for the reader to answer.

Let’s look at a very practical example: there are two directories, and each directory has a large number of files, and the files in both directories are found. At this time, using Set is much faster than List. Because the underlying implementation of Set is a hash table, to determine whether an element exists in a certain set, the time complexity of List is O(n), and the time complexity of Set is O(1), so Set should be used here. We should be very clear about the time complexity of each commonly used data structure in Python, and make full use of the advantages of different data structures in the process of actually writing code.

5. else in Python

Finally, let’s look at an example of understanding the advantages and disadvantages of the Python language, that is, the two elses added in Python. Compared with C++ language or Java language, there are two more elses in Python syntax.

One in a while loop or a for loop:

 while True:
        ....
    else:
        ....

The other is in a try…except statement:

 try:
        ....
    except:
        ....
    else:
        ....
    finally:
       ....

So, which one is good design and which one is bad design? To answer this question, let’s first look at what role the else statement plays in everyone’s inherent concepts. In all languages, else appears with an if statement:

 if <condition>
        statement1
    else
        statement2

Translated into natural language, if the condition is met, execute statement 1, otherwise, execute statement 2. Pay attention to the term we used earlier, it is otherwise, that is to say, the else statement plays the role of “otherwise” in our inherent concept, and it is executed only when the conditions are not met.

Let’s look at the else statement after the while loop in Python. The else statement is executed when the while statement completes normally. Therefore, according to the semantics, the else function of the while loop is and. In other words, in Python, it is more appropriate to replace the else at the end of the while loop with and.

You may think that I am a bit of a dead end, well, let me emphasize again, the else statement in the while loop is executed when the loop ends normally, so I would like to ask:

1. If a break statement is encountered in the while loop, will the else statement be executed?

2. If the continue statement is encountered at the end of the while loop, will the else statement still be executed?

3. If an exception occurs inside the while loop, will the else statement still execute?

Here are a few questions that most people can’t answer correctly quickly. And our code is written for people to read, and most people should not be excluded from being able to understand this code. So I think the else statement at the end of the loop statement in the Python language is a bad design.

Now, let’s look at the else in the try…except statement. This else is very well designed, and other languages should also learn from this design. The semantics of this design is to execute the statement in try, and the statement in it may be abnormal. If there is an exception, execute the statement in except. If there is no exception, execute the statement in else. Finally, no matter whether it occurs or not If there is an exception, the finally statement must be executed. The good thing about this design is that the else statement is exactly the same as our intuitive feeling, and it is executed without exception. Moreover, having an else is better than not having an else. With else, the code that the programmer thinks may be abnormal is correctly separated from the code that is unlikely to be abnormal. In this way, it is more clearly indicated which statement may be abnormal. More exposure of the programmer’s intentions makes code maintenance and modification easier.

Conclusion: My answer is quite long, but I believe it will be helpful to many people. What I want to say here is that Python is a programming language with a wide range of uses. You should not pursue proficiency in the Python programming language itself, but focus on the actual problems you need to solve. Secondly, the vast majority of people have misunderstandings about Python. They think that Python is very simple. After the answer, you can go back and learn Python again. Finally, for the doubts of some students – the recruitment position requires proficiency in Python, my answer is that they do not expect to recruit a person who is proficient in Python, they just want to recruit a qualified engineer, and most Python engineers, , No, yes, grid!

I know you are interested in python, so I have prepared the following information for you~

This full version of the full set of learning materials for Python has been uploaded. If you need it, you can click the link to get it for free or scroll to the end to scan the QR code 100% free]

Free sharing of python learning resources, guaranteed 100% free! ! !

If you need it, you can click herePython learning route (2023 revised version) with related materials (safe link, click with confidence)

There are benefits at the end of the article~

1. Learning routes in all directions of Python

The technical points in all directions of Python are sorted out to form a summary of knowledge points in various fields. Its usefulness lies in that you can find corresponding learning resources according to the above knowledge points to ensure that you can learn more comprehensively.

2. Essential Python development tools

3. Excellent Python learning books

When I have learned a certain foundation and have my own understanding ability, I will read some books or handwritten notes compiled by my predecessors. These notes record their understanding of some technical points in detail. These understandings are relatively unique and can be learned. to a different way of thinking.

Fourth, Python video collection

Watching the zero-based learning video is the fastest and most effective way to learn. Following the teacher’s ideas in the video, it is still very easy to get started from the basics to the in-depth.

V. Practical cases

Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.

6. Python exercises

Check the learning results.

7. Interview materials

We must learn Python to find high-paying jobs. The following interview questions are the latest interview materials from first-line Internet companies such as Ali, Tencent, and Byte, and Ali bosses have given authoritative answers. After finishing this set The interview materials believe that everyone can find a satisfactory job.

This full version of the full set of Python learning materials has been uploaded. If you need it, you can scan the CSDN official certification QR code below or click the link to get it for free [Guaranteed 100% free ]
The Python learning route (2023 revised version) is accompanied by the related materials “Python Learning Materials”, which have been packaged and picked up by yourself [ps: the materials that need to be collected (please note clearly, search and send them to you)]. Because the link is often https://mp.weixin.qq.com/s/UVxw0daFCgAMFhz9cfrjAQicon-default.png?t=N2N8https://mp.weixin.qq.com/s/UVxw0daFCgAMFhz9cfrjAQ

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledgePython entry skill treeHomepageOverview 258490 people are learning systematically