Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the advanced skills of Python development?

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What are the advanced skills of Python development? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

I list a few advanced Python tips:

1. Contextmanager

When writing Python code, you often put a series of operations in a statement block, and Python 2.5 adds with syntax to implement context management, which makes the code more readable and fewer errors. The most common example is open, and if you don't use with, you would use open like this:

If you use with, you can simplify it to two lines:

The file is automatically closed after the indented block of code is executed. Creating a context manager is actually creating a class, adding the _ _ enter__ and _ _ exit__ methods. Take a look at how to implement the context management capabilities of open:

Customizing the context manager is really convenient, but the Python standard library also provides an easier-to-use context manager tool module, contextlib, which is implemented through a generator, so we no longer have to create classes and two special methods, _ _ enter__ and _ _ exit__:

The yield keyword divides the context into two parts: the code block in _ _ init__ before yield; the code block in _ _ exit__ after yield; and the value generated by yield is bound to the variable in the as clause of the with statement (if it is not generated, there is no as sentence).

2. Total_ordering . To compare custom objects, you need to add _ _ lt__, _ _ le__, _ _ gt__, _ _ ge__ and _ _ eq__ methods. If you use total_ordering, you only need to define _ _ eq__ and one of _ _ lt__, _ _ le__, _ _ gt__, _ _ ge__:

3. Sometimes BUG is hidden so deep that it needs a clear presentation of the context to help determine. Debugging with pdb is not convenient, and it is not intuitive to use print. You can use the following function to get the current call stack:

4. Inspect . Sometimes we want to look at some information about the object or do type checking, that is, introspection (check something to determine what it is, what it knows, and what it can do):

What meaning can it have in practical work? get the parameters of the method through introspection, so as to set the cache key, such as flask-cache (https://github.com/thadeusb/flask-cache/blob/master/flask_cache/__init__.py#L418) and douban-mc (https://github.com/douban/douban-mc/blob/master/douban/mc/decorator.py#L39)).

5. Mixin mode.

If we want to store data through the python built-in type Dict, we can write classes that can be mixed with DictMixin:

PS: if you want to be compatible with Python 2.6 and Python 3, you can use collections.MutableMapping:

But MutableMapping requires additional implementations of _ _ iter__ and _ _ len__.

It inherits Iterable and Sized, while Iterable requires you to define the _ _ iter__ method through abstractmethod, and Sized requires you to define the _ _ len__ method, otherwise you will be prompted:

TypeError: Can't instantiate abstract class MyDict with abstract metho

Ds _ _ iter__, _ _ len__

After reading the above, have you mastered the advanced skills of Python development? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report