This is the 23rd day of my participation in Gwen Challenge
Well, this is the sequel to the article rrshift, the Python magic method, how to implement Pythonic’s graphically concise code from the long nested implementation?
Take the subway (train into the station (wait at the platform) swipe your card into the station (pass security check (go to the subway station ()))))))Copy the code
RidingRoad is now the Pythonic path
Go to the subway station >> pass the security check >> swipe your card to enter the station >> Go to the platform to wait for the train >> Train to enter the station >> take the subwayCopy the code
On the first code
# -*- coding: utf-8 -*-
import functools
class Pipe:
def __init__(self, function) :
self.function = function
functools.update_wrapper(self, function)
def __rrshift__(self, other) :
return self.function(other)
Go to the subway station >> pass the security check >> swipe your card to enter the station >> Go to the platform to wait for the train >> Train to enter the station >> take the subway
def access_to_MTR_station() :
print("1. RidingRoad to MTR station")
# security_check = Pipe(security_check)
@Pipe
def security_check(other) :
print("2. RidingRoad through security")
@Pipe
def payment_entry(other) :
print("3. Swipe your card into RidingRoad")
@Pipe
def waiting_for_subway(other) :
print("4. RidingRoad to the platform.")
@Pipe
def train_come(other) :
print("5. The train enters the station")
@Pipe
def take_the_subway(other) :
print("6. RidingRoad subway ride home to the white picket fence of his wife and kids.")
Go to the subway station >> pass the security check >> swipe your card to enter the station >> Go to the platform to wait for the train >> Train to enter the station >> take the subway
if __name__ == "__main__":
access_to_MTR_station() >> security_check \
>> payment_entry >> waiting_for_subway \
>> train_come >> take_the_subway
# \ is used to wrap long lines of code, the top is the same as the bottom
# access_to_MTR_station() >> security_check >> payment_entry >> waiting_for_subway >> train_come >> take_the_subway
Copy the code
Really? See the effect
**************************************** Are you kidding me? * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 1, RidingRoad to subway station 2, RidingRoad through security 3, RidingRoad charge station 4, RidingRoad to waiting 5, train platform 6, take the subway home to his wife and children hot kang RidingRoad * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Believe it or not. All you need to do is take a try. ****************************************Copy the code
Is it a lot of questions? Question 1: Function calls without parentheses? Question 2: Why do you need to pass an other argument when there is no argument in the function definition?
A review of the rules of >> and << : See rrshift, a Python magic method, for a detailed example in the previous article
-
Commands start from left to right, whether moved left or right. The output of the first combination is used as the left operator of the next operation;
-
The parentheses change the order of operations as expected
-
The __rshift__ and __lshift__ operations are called only on the class that implements them
-
If one of the classes does not implement __rshift__ and _lshift_, the __rrshift__ or __rlshift__ operation is called, itself as self
Answer to Question 1
I’m sure you have to put parentheses on a function call and the reason why I didn’t put parentheses on a function call is because __rrshift__ is called why is _rrshift_ called? Back to rule 4: If one of the classes does not implement __rshift__ and _lshift_, then the __rrshift__ or __rlshift__ operation is called, itself as self
1. Security_check has the Pipe decorator added to it. Security_check = Pipe(security_check) 2, access_to_MTR_station() >> security_check Access_to_MTR_station () is the return value of a normal function, in this case None. Since None is just a value, security_check will call __rrshift__’s function 4, i.e. Self.function (other), 5, that is: security_check(None) the same with others
Answer to Question 2
The output of the first combination will be used as the left operator of the next operation.
Func1 >> func2 1, the result of func1 will be a parameter to func2, 2, that is, func2 must receive a parameter 3, so the latter function must need a formal parameter to receive this parameter 4, here we receive other, although we don’t use it
Why not take parameters? I’m going to continue in the next video and I’m going to do a little bit more advanced in the next video, where I’m going to pass in parameters and I’m going to tell you a little bit about the plot, but I’m going to need an anonymous function