PYTHONPORTAL Telegram 4503
Аналогично Unix-пайпам, в Python можно строить цепочки функций, переопределяя метод __or__

Это позволяет связывать операции так, что вывод одной функции становится входом для следующей → чисто, читаемо и удобно для композиции

Именно так LangChain реализует элегантные пайплайны вроде: chat_prompt | groq_model | output_parser

Пример:

class Pipe:
def __init__(self, func):
self.func = func

def __call__(self, x):
return self.func(x)

def __or__(self, other):
return self.__class__(lambda x: other(self(x)))


🔸Pipe — обёртка над функцией

🔸__or__ (|) позволяет комбинировать пайпами, как в Unix

🔸__call__ делает объект вызываемым, как обычную функцию

Теперь можно обернуть любые функции в Pipe и комбинировать их в цепочку:

double = Pipe(lambda x: x * 2)
square = Pipe(lambda x: x ** 2)

pipeline = double | square
print(pipeline(3)) # (3 * 2) ** 2 = 36


👉 @PythonPortal
Please open Telegram to view this post
VIEW IN TELEGRAM



tgoop.com/PythonPortal/4503
Create:
Last Update:

Аналогично Unix-пайпам, в Python можно строить цепочки функций, переопределяя метод __or__

Это позволяет связывать операции так, что вывод одной функции становится входом для следующей → чисто, читаемо и удобно для композиции

Именно так LangChain реализует элегантные пайплайны вроде: chat_prompt | groq_model | output_parser

Пример:

class Pipe:
def __init__(self, func):
self.func = func

def __call__(self, x):
return self.func(x)

def __or__(self, other):
return self.__class__(lambda x: other(self(x)))


🔸Pipe — обёртка над функцией

🔸__or__ (|) позволяет комбинировать пайпами, как в Unix

🔸__call__ делает объект вызываемым, как обычную функцию

Теперь можно обернуть любые функции в Pipe и комбинировать их в цепочку:

double = Pipe(lambda x: x * 2)
square = Pipe(lambda x: x ** 2)

pipeline = double | square
print(pipeline(3)) # (3 * 2) ** 2 = 36


👉 @PythonPortal

BY Python Portal




Share with your friend now:
tgoop.com/PythonPortal/4503

View MORE
Open in Telegram


Telegram News

Date: |

How to create a business channel on Telegram? (Tutorial) During the meeting with TSE Minister Edson Fachin, Perekopsky also mentioned the TSE channel on the platform as one of the firm's key success stories. Launched as part of the company's commitments to tackle the spread of fake news in Brazil, the verified channel has attracted more than 184,000 members in less than a month. 4How to customize a Telegram channel? To view your bio, click the Menu icon and select “View channel info.” With Bitcoin down 30% in the past week, some crypto traders have taken to Telegram to “voice” their feelings.
from us


Telegram Python Portal
FROM American