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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:36:41+00:00 2026-06-04T14:36:41+00:00

As a learning exercise, I’m trying to implement a class which will emulate the

  • 0

As a learning exercise, I’m trying to implement a class which will emulate the behavior of python’s complex builtin, but with different behavior of the __str__ and __repr__ methods: I want them to print in the format…

(1.0,2.0)

…instead of:

(1+2j)

I first tried simply subclassing from complex and redefining __str__ and __repr__, but this has the problem that when non-overridden methods are called, a standard complex is returned, and printed in the standard format:

>>> a = ComplexWrapper(1.0,1.0)
>>> a
(1.0,1.0)
>>> b = ComplexWrapper(2.0,3.0)
>>> b
(2.0,3.0)
>>> a + b
(3+4j)

When the desired output is (3.0,4.0).

I was reading about metaclasses and thought they would solve my problem. Starting from the answer in Python Class Decorator, my current implementation is as follows:

def complex_str(z):
    return '(' + str(z.real) + ',' + str(z.imag) + ')'
def complex_repr(z):
    return '(' + repr(z.real) + ',' + repr(z.imag) + ')'

class CmplxMeta(type):
    def __new__(cls, name, bases, attrs):
        attrs['__str__'] = complex_str
        attrs['__repr__'] = complex_repr
        return super(CmplxMeta, cls).__new__(cls, name, bases, attrs)

class ComplexWrapper(complex):
    __metaclass__ = CmplxMeta

Unfortunately, this seems to have the same behavior as the previous solution (e.g. when two ComplexWrapper instances are added to each other).

I admit, I don’t fully understand metaclasses. Maybe my problem can be solved in a different way?

Of course, I could manually redefine the relevant methods such as __add__, __subtract__, etc. But that would be very repetitive, so I would prefer a more elegant solution.

Any help appreciated.


EDIT: Response to agf’s answer:

So a number of things I don’t understand about your code:

  1. Where does the __new__ method of the ReturnTypeWrapper metaclass get its arguments from? If they are passed automatically, I would expect in this case that name = "Complex", bases = (complex), dict = {}. Is that correct? Is this method of automatic passing of class data specific to metaclasses?

  2. Why do you use
    cls = type.__new__(mcs, name, bases, dct) instead of
    cls = type(mcs, name, bases, dct)?
    Is it just to avoid confusion with the “other meaning” of type()?

  3. I copied your code, and added my special implementations of __str__ and __repr__ in your ComplexWrapper class. But it doesn’t work; printing any object of type Complex just prints in the standard Python format. I don’t understand that, as the two methods should have been picked up in the for loop of the metaclass, but should have been overridden by my definitions afterward.

The relevant section of my code:

class Complex(complex):
    __metaclass__ = ReturnTypeWrapper
    wrapped_base = complex
    def __str__(self):
        return '(' + str(self.real) + ',' + str(self.imag) + ')'
    def __repr__(self):
        return '(' + repr(self.real) + ',' + repr(self.imag) + ')'

And its behavior:

>>> type(a)
<class 'Cmplx2.Complex'>
>>> a.__str__
<bound method Complex.wrapper of (1+1j)>
>>> a.__str__()
'(1+1j)'
>>> 

Thanks again for your answer and feel free to edit/remove the above if you address them in your answer!

  • 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-04T14:36:42+00:00Added an answer on June 4, 2026 at 2:36 pm

    Your current approach won’t work. How you define your class isn’t the issue — the methods of complex are creating new instances of complex when you call them, rather than using the type of the input objects. You’ll always get back instances of complex rather than ComplexWrapper, so your customized methods won’t be called:

    >>> type(ComplexWrapper(1.0,1.0) + ComplexWrapper(2.0,3.0))
    <type 'complex'>
    

    Instead, you need to convert the new complex objects returned by the methods of complex to return objects of the derived class.

    This metaclass wraps all the methods of the specified base class and attaches the wrapped methods to the class. The wrapper checks if the value to be returned is an instance of the base class (but excluding instances of subclasses), and if it is, converts it to an instance of the derived class.

    class ReturnTypeWrapper(type):
        def __new__(mcs, name, bases, dct):
            cls = type.__new__(mcs, name, bases, dct)
            for attr, obj in cls.wrapped_base.__dict__.items():
                # skip 'member descriptor's and overridden methods
                if type(obj) == type(complex.real) or attr in dct:
                    continue
                if getattr(obj, '__objclass__', None) is cls.wrapped_base:
                    setattr(cls, attr, cls.return_wrapper(obj))
            return cls
    
        def return_wrapper(cls, obj):
            def convert(value):
                return cls(value) if type(value) is cls.wrapped_base else value
            def wrapper(*args, **kwargs):
                return convert(obj(*args, **kwargs))
            wrapper.__name__ = obj.__name__
            return wrapper
    
    class Complex(complex):
        __metaclass__ = ReturnTypeWrapper
        wrapped_base = complex
        def __str__(self):
            return '({0}, {1})'.format(self.real, self.imag)
        def __repr__(self):
            return '{0}({1!r}, {2!r})'.format(self.__class__.__name__, 
                                              self.real, self.imag)
    
    
    a = Complex(1+1j)
    b = Complex(2+2j)
    
    print type(a + b)
    

    Note that this won’t wrap the __coerce__ special method, since it returns a tuple of complexs; the wrapper can easily be converted to look inside sequences if necessary.

    The __objclass__ attribute of unbound methods seems to be undocumented, but it points to the class the method is defined on, so I used it to filter out methods defined on classes other than the one we’re converting from. I also use it here to filter out attributes that aren’t unbound methods.

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

Sidebar

Related Questions

I'm trying (as a self-learning exercise) to create a Clojure macro that will generate
I am writing a huffman implementation in Python as a learning exercise. I have
I'm currently learning python and trying to do exercises at pyschools (if anyone knows
I'm new to OCaml and attempting to implement List.append as a learning exercise. This
This is a learning exercise in expression trees. I have this working code: class
I have been making a matrix class (as a learning exercise) and I have
I'm developing a Python web app as a learning exercise, and I am looking
Im quite new to generics and as a learning exercise Im trying to create
I'm writing an RSS reader in python as a learning exercise, and I would
I'm brand new to MVC and, as a learning exercise, I'm trying to re-write

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.