Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __radd__ method to ParameterExpression class #148

Merged
merged 1 commit into from
Mar 25, 2024

Conversation

YuChenSSR
Copy link
Collaborator

@YuChenSSR YuChenSSR commented Mar 25, 2024

What does this PR do?

This pull request adds the radd special method to the ParameterExpression class. This allows ParameterExpression objects to support addition with integers and other numeric types when the ParameterExpression is on the right-hand side of the + operator.

Why is this change needed?

Previously, adding an integer to a ParameterExpression (e.g., 1 + param_expr) would raise a TypeError because the integer's add method doesn't know how to add a ParameterExpression. By implementing radd, we define the behavior for this case and allow the addition to complete successfully by delegating to the ParameterExpression's add method.
This change improves the usability and consistency of the ParameterExpression class by supporting the commutative property of addition.

How does it work?
The radd method is called when a ParameterExpression appears on the right side of the + operator and the left operand doesn't know how to handle the addition.
In our implementation, radd simply delegates to the existing __add__ method:

def __radd__(self, r):
    return self + r

@Zhaoyilunnn Zhaoyilunnn merged commit 76c55db into ScQ-Cloud:master Mar 25, 2024
23 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants