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 8198391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:56:28+00:00 2026-06-07T05:56:28+00:00

I really like the syntax of the magic methods or whatever they are called

  • 0

I really like the syntax of the “magic methods” or whatever they are called in Python, like

class foo:
    def __add__(self,other): #It can be called like c = a + b
        pass

The call

c = a + b

is then translated to

a.__add__(b)

Is it possible to mimic such behaviour for “non-magic” functions? In numerical computations I need the Kronecker product, and am eager to have “kron” function such that

kron(a,b) 

is in fact

a.kron(b)?

The use case is: I have two similar classes, say, matrix and vector, both having Kronecker product. I would like to call them

a = matrix()
b = matrix()
c = kron(a,b)

a = vector()
b = vector()
c = kron(a,b)

matrix and vector classes are defined in one .py file, thus share the common namespace. So, what is the best (Pythonic?) way to implement functions like above? Possible solutions:

1) Have one kron() functions and do type check

2) Have different namespaces

3) ?

  • 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-06-07T05:56:30+00:00Added an answer on June 7, 2026 at 5:56 am

    The python default operator methods (__add__ and such) are hard-wired; python will look for them because the operator implementations look for them.

    However, there is nothing stopping you from defining a kron function that does the same thing; look for __kron__ or __rkron__ on the objects passed to it:

    def kron(a, b):
        if hasattr(a, '__kron__'):
            return a.__kron__(b)
        if hasattr(b, '__rkron__'):
            return b.__rkron__(a)
        # Default kron implementation here
        return complex_operation_on_a_and_b(a, b)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really like this syntax, though I forgot what it was called @notifications.delete_if{|x| x
I really like the following template to create properties for class in VS2005 Type
I'm making a blog in PHP and I'd really like to have syntax highlighting
I really like the anonymous type syntax. e.g. new {a = 1, b =
I really like the magento structure but finding things is very hard ;) My
I really like the MVC way and have actually enjoyed learning ASP.NET MVC (I
I really like the IIS7 URL rewriting module and so far, it worked great
I really like to have some graphic on my startup/login screen when starting my
I really like the visual effect where a new element seems to push an
I really like the promote to home page feature of drupal. But what if

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.