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

How to implement chain call in Python

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to implement chain call in Python? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

When we query the database using Django's models, we can see that it is written like this:

Form app.models import XXX

Query = XXX.objects.all ()

Query = query.filter (name=123, age=456) .filter (salary=999)

In this way, the query object has a filter method, and the returned data of this method can continue to call the filter method, which can be called indefinitely.

How does this writing come true?

If we write a method of a class directly, see if we can call it like this:

Class Query:

Def filter (self):

Pass

Query = Query ()

Query.filter () .filter ()

Calling filter again directly on the result returned by query.filter () will result in an error. This is because the method returns None when the return statement is not explicitly written, and the None object has no so-called filter method.

So what has a filter method? Obviously our query object has a filter method. So how do you get this method to return itself as an object?

At this point, we need to take a look at the first parameter self that we always write when defining class methods. It is found in almost every class method. You only know that you can use self.xxx () when calling class methods in a class and self.yy when calling class properties. Have you ever wondered what would happen if this thing was used alone?

In fact, self refers to the object itself after the class is instantiated into an object. And this object obviously has a filter method. So let's modify the filter method to return self:

Class Query:

Def filter (self):

Return self

Query = Query ()

Query.filter () .filter ()

As can be seen from the picture, there is no mistake now. So back to the original question, how is the chained call in Django implemented to pass in query parameters?

In fact, there is a problem of lazy query.

When we keep calling the .filter () method, Django will cache all these query conditions, and only when we need to get the results, or query how many pieces of data that meet the criteria, will it really connect to the database to query.

So here we are going to simulate this environment and cache the query conditions.

So in order to get the parameter name passed in when the method is called, we will use the * * kwargs parameter. This parameter can accept all parameters in the form of key=value:

Class Query ():

Def _ init__ (self):

Self.query_condition = {}

Def filter (self, * * kwargs):

Self.query_condition.update (kwargs)

Return self

Query = Query ()

A = query.filter (name='kingname') .filter (age__gt=15, address='yyyyyy') .filter (salary=99999)

Print (query.query_condition)

The running effect is shown in the following figure:

After reading the above, have you mastered the method of how to implement chained calls in Python? 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

Internet Technology

Wechat

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

12
Report