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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:44:43+00:00 2026-06-05T11:44:43+00:00

I have a class that defines the __complex__ special method. My class is not

  • 0

I have a class that defines the __complex__ special method. My class is not a standard numeric type (int, float, etc.), but it behaves like one as I have special methods defined for __add__, __sub__, etc.

I would like __complex__ to return my complex valued numeric object, not a standard complex numeric value python expects. As such, Python throws the following error when I try to return my object, rather than a standard complex numeric.

TypeError: unsupported operand type(s) for *: ‘complex’ and
‘MyNumericClass’

What’s the best way to do this?


Edit:

# Python builtins
import copy
# Numeric python
import numpy as np

class MyNumericClass (object):
    """ My numeric class, with one single attribute """
    def __init__(self, value):
        self._value = value

    def __complex__(self):
        """ Return complex value """
        # This looks silly, but my actual class has many attributes other
        # than this one value.
        self._value = complex(self._value)
        return self

def zeros(shape):
    """
    Create an array of zeros of my numeric class

    Keyword arguments:
      shape -- Shape of desired array
    """
    try:
        iter(shape)
    except TypeError, te:
        shape = [shape]
    zero = MyNumericClass(0.)
    return fill(shape, zero)

def fill(shape, value):
    """
    Fill an array of specified type with a constant value

    Keyword arguments:
      shape -- Shape of desired array
      value -- Object to initialize the array with
    """
    try:
        iter(shape)
    except TypeError, te:
        shape = [shape]
    result = value
    for i in reversed(shape):
        result = [copy.deepcopy(result) for j in range(i)]
    return np.array(result)

if __name__ == '__main__':
    a_cplx = np.zeros(3).astype(complex)
    print a_cplx
    b_cplx = zeros(3).astype(complex)
    print b_cplx
  • 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-05T11:44:45+00:00Added an answer on June 5, 2026 at 11:44 am

    A couple of options:

    1. Define __rmul__ (or define __mul__ and flip the multiplication operands).
    2. Cast your MyNumericClass instance to complex before multiplying.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that defines its own enum like this: public class Test
I have two different projects and in one, I have a class that defines
I have a Literal class that defines a text value and an ID. I
I have an AbstractEntity class as superclass for all my entites that defines an
I have a simple web service method that returns a simple java class as
I have a class that defined a user defined operator for a TCHAR*, like
I have a class Bar that has a user-defined list of config keys and
I have a functional AdjacencyListGraph class that adheres to a defined interface GraphStructure. In
I have a class with defined functions that need to be passed as parameters.
I have these two simple files that define a C++ class with a tryME

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.