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

  • Home
  • SEARCH
  • 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 8819963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:29:35+00:00 2026-06-14T05:29:35+00:00

Say I have this class: class MyString(str): def someExtraMethod(self): pass I’d like to be

  • 0

Say I have this class:

class MyString(str):
    def someExtraMethod(self):
        pass

I’d like to be able to do

a = MyString("Hello ")
b = MyString("World")
(a + b).someExtraMethod()
("a" + b).someExtraMethod()
(a + "b").someExtraMethod()
  1. Running as is:

    AttributeError: 'str' object has no attribute 'someExtraMethod'
    
  2. Obviously that doesn’t work. So I add this:

    def __add__(self, other):
        return MyString(super(MyString, self) + other)
    def __radd__(self, other):
        return MyString(other + super(MyString, self))
    
    TypeError: cannot concatenate 'str' and 'super' objects
    
  3. Hmmm, ok. super doesn’t appear to respect operator overloading. Perhaps:

    def __add__(self, other):
        return MyString(super(MyString, self).__add__(other))
    def __radd__(self, other):
        return MyString(super(MyString, self).__radd__(other))
    
    AttributeError: 'super' object has no attribute '__radd__'
    

Still no luck. What should I be doing here?

  • 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-14T05:29:36+00:00Added an answer on June 14, 2026 at 5:29 am

    I usually use the super(MyClass, self).__method__(other) syntax, in your case this does not work because str does not provide __radd__. But you can convert the instance of your class into a string using str.

    To who said that its version 3 works: it does not:

    >>> class MyString(str):
    ...     def __add__(self, other):
    ...             print 'called'
    ...             return MyString(super(MyString, self).__add__(other))
    ... 
    >>> 'test' + MyString('test')
    'testtest'
    >>> ('test' + MyString('test')).__class__
    <type 'str'>
    

    And if you implement __radd__ you get an AttributeError(see the note below).

    Anyway, I’d avoid using built-ins as base type. As you can see some details may be tricky, and also you must redefine all the operations that they support, otherwise the object will become an instance of the built-in and not an instance of your class.
    I think in most cases it’s easier to use delegation instead of inheritance.

    Also, if you simply want to add a single method, then you may try to use a function on plain strings instead.


    I add here a bit of explanation about why str does not provide __radd__ and what’s going on when python executes a BINARY_ADD opcode(the one that does the +).

    The abscence of str.__radd__ is due to the fact that string objects implements the concatenation operator of sequences and not the numeric addition operation. The same is true for the other sequences such as list or tuple. These are the same at “Python level” but actually have two different “slots” in the C structures.

    The numeric + operator, which in python is defined by two different methods(__add__ and __radd__) is actually a single C function that is called with swapped arguments to simulate a call to __radd__.

    Now, you could think that implementing only MyString.__add__ would fix your problem, since str does not implement __radd__, but that’s not true:

    >>> class MyString(str):
    ...     def __add__(self, s):
    ...             print '__add__'
    ...             return MyString(str(self) + s)
    ... 
    >>> 'test' + MyString('test')
    'testtest'
    

    As you can see MyString.__add__ is not called, but if we swap arguments:

    >>> MyString('test') + 'test'
    __add__
    'testtest'
    

    It is called, so what’s happening?

    The answer is in the documentation which states that:

    For objects x and y, first x.__op__(y) is tried. If this is not
    implemented or returns NotImplemented, y.__rop__(x) is tried. If this
    is also not implemented or returns NotImplemented, a TypeError
    exception is raised. But see the following exception:

    Exception to the previous item: if the left operand is an instance of
    a built-in type or a new-style class, and the right operand is an
    instance of a proper subclass of that type or class and overrides the
    base’s __rop__() method, the right operand’s __rop__() method is tried
    before the left operand’s __op__() method.

    This is done so that a subclass can completely override binary
    operators. Otherwise, the left operand’s __op__() method would always
    accept the right operand: when an instance of a given class is
    expected, an instance of a subclass of that class is always
    acceptable.

    Which means you must implement all the methods of str and the __r*__ methods, otherwise you’ll still have problems with argument order.

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

Sidebar

Related Questions

So, say I have models like this: class Foo(Model): name = CharField(max_length=200) def latest_comment(self):
Let's say I have this : class whatever(object): def __init__(self): pass and this function:
Say I have code like this: class Car def test_drive!; end end class AssemblyLine
Let's say I have this class: class ComponentLibrary def self.register(klass); ...; end end And
So let's say you have something like this: Class Person: height = 6' 0
lets say I have a class like this: public class MyClass { private String
Say I have this class: class myclass { public int Field1{ get; set; }
Say I have this class: public class Account { public int AccountID { get;
say I have this class: class animal { function noise() { print 'woof'; }
Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }

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.