Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7611371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:39:13+00:00 2026-05-31T01:39:13+00:00

According to the Cython documentation regarding arithmetic special methods (operator overloads), the way they’re

  • 0

According to the Cython documentation regarding arithmetic special methods (operator overloads), the way they’re implemented, I can’t rely on self being the object whose special method is being called.

Evidently, this has two consequences:

  1. I can’t specify a static type in the method declaration. For example, if I have a class Foo which can only be multiplied by, say, an int, then I can’t have def __mul__(self, int op) without seeing TypeErrors (sometimes).
  2. In order to decide what to do, I have to check the types of the operands, presumably using isinstance() to handle subclasses, which seems farcically expensive in an operator.

Is there any good way to handle this while retaining the convenience of operator syntax? My whole reason for switching my classes to Cython extension types is to improve efficiency, but as they rely heavily on the arithmetic methods, based on the above it seems like I’m actually going to make them worse.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:39:14+00:00Added an answer on May 31, 2026 at 1:39 am

    If I understand the docs and my test results correctly, you actually can have a fast __mul__(self, int op) on a Foo, but you can only use it as Foo() * 4, not 4 * Foo(). The latter would require an __rmul__, which is not supported, so it always raises TypeError.

    The fact that the second argument is typed int means that Cython does the typecheck for you, so you can be sure that the left argument is really self.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to
According to Apple's documentation on the View Controller Lifecycle I noticed the following regarding
According to the documentation found in http://ask.github.com/celery/userguide/routing.html#manual-routing i can pass a queue parameter to
According to what I have found so far, I can use the following code:
According to Steffen's post this is an efficient way to generate random BOOL s
According to the documentation , socket_read() is supposed to return FALSE when the remote
According to be Spring Framework documentation, logging is the only mandatory dependency of Spring.
According to MDN documentation for JSON.stringify : Properties of non-array objects are not guaranteed
According to the PHP Documentation PDO::prepare() adds quotes to all your parameters so that
According to the Ruby Set class's documentation, == Returns true if two sets are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.