How to subclass str in Python
Asked Answered
W

5

52

I am trying to subclass str object, and add couple of methods to it. My main purpose is to learn how to do it. Where I am stuck is, am I supposed to subclass string in a metaclass, and create my class with that meta, or subclass str directly?

And also, I guess I need to implement __new__() somehow, because, my custom methods will modify my string object, and will return new mystr obj.

My class's methods, should be completely chainable with str methods, and should always return a new my class instance when custom methods modified it. I want to be able to do something like this:

a = mystr("something")
b = a.lower().mycustommethod().myothercustommethod().capitalize()
issubclass(b,mystr) # True

I want to have it all the abilities that a str have. For example, a = mystr("something") then I want to use it like, a.capitalize().mycustommethod().lower()

It is my understanding that, I need to implement __new__(). I think so because, strings methods would probably try to create new str instances. So , if I overwrite __new__(), They supposedly would return my custom str class. However, I don't know how to pass arguments to my custom class's __init__() method in that case. And I guess I would need to use type() in order to create a new instance in __new__() method right?

Waylonwayman answered 31/8, 2011 at 10:28 Comment(1)
@yasar11732: As sth points out below, a.capitalize() will return a standard, unmodified str, not your custom class, so a.capitalize().mycustommethod() will fail. It is far, far better coding practice to just write a couple functions and just do mycustommethod(a.capitalize()).lower(), because this will not confuse everyone else who reads your code (BTW, "everyone else" includes "you, two years from now").Langsyne
I
51

Overwriting __new__() works if you want to modify the string on construction:

class caps(str):
   def __new__(cls, content):
      return str.__new__(cls, content.upper())

But if you just want to add new methods, you don't even have to touch the constructor:

class text(str):
   def duplicate(self):
      return text(self + self)

Note that the inherited methods, like for example upper() will still return a normal str, not text.

Ita answered 31/8, 2011 at 10:39 Comment(1)
More generic approach based on __init__(self, *args, **kwargs) + super() is reporting DeprecationWarning: object.__init__() takes no parameters. Thus, I assume __new__(cls, *args, **kwargs) is a better way to do it. Or? Maybe this explains better: jfine-python-classes.readthedocs.org/en/latest/…Armour
O
42

I am trying to subclass str object, and add couple of methods to it. My main purpose is to learn how to do it.

UserString was created before it was possible to subclass str directly, so prefer to subclass str, instead of using UserString (as another answer suggests).

When subclassing immutable objects, it's usually necessary to modify the data before you instantiate the object - therefore you need to both implement __new__ and call the parent __new__ (preferably with super, instead of str.__new__ as another answer suggests).

In Python 3, it is more performant to call super like this:

class Caps(str):
    def __new__(cls, content):
        return super().__new__(cls, content.upper())

__new__ looks like a class method, but it is actually implemented as a static method, so we need to pass cls redundantly as the first argument. We don't need the @staticmethod decorator, however.

If we use super like this to support Python 2, we'll note the redundant cls more clearly:

class Caps(str):
    def __new__(cls, content):
        return super(Caps, cls).__new__(cls, content.upper())

Usage:

>>> Caps('foo')
'FOO'
>>> isinstance(Caps('foo'), Caps)
True
>>> isinstance(Caps('foo'), str)
True

The complete answer

None of the answers so far does what you've requested here:

My class's methods, should be completely chainable with str methods, and should always return a new my class instance when custom methods modified it. I want to be able to do something like this:

a = mystr("something")
b = a.lower().mycustommethod().myothercustommethod().capitalize()
issubclass(b,mystr) # True

(I believe you mean isinstance(), not issubclass().)

You need a way to intercept the string methods. __getattribute__ does this.

class Caps(str):
    def __new__(cls, content):
        return super().__new__(cls, content.upper())
    def __repr__(self):
        """A repr is useful for debugging"""
        return f'{type(self).__name__}({super().__repr__()})'
    def __getattribute__(self, name):
        if name in dir(str): # only handle str methods here
            def method(self, *args, **kwargs):
                value = getattr(super(), name)(*args, **kwargs)
                # not every string method returns a str:
                if isinstance(value, str):
                    return type(self)(value)  
                elif isinstance(value, list):
                    return [type(self)(i) for i in value]
                elif isinstance(value, tuple):
                    return tuple(type(self)(i) for i in value)
                else: # dict, bool, or int
                    return value
            return method.__get__(self) # bound method 
        else: # delegate to parent
            return super().__getattribute__(name)
    def mycustommethod(self): # shout
        return type(self)(self + '!')
    def myothercustommethod(self): # shout harder
        return type(self)(self + '!!')

and now:

>>> a = Caps("something")
>>> a.lower()
Caps('SOMETHING')
>>> a.casefold()
Caps('SOMETHING')
>>> a.swapcase()
Caps('SOMETHING')
>>> a.index('T')
4
>>> a.strip().split('E')
[Caps('SOM'), Caps('THING')]

And the case requested works:

>>> a.lower().mycustommethod().myothercustommethod().capitalize()
Caps('SOMETHING!!!')

Response to Comment

Why is the Python 3 only call, i.e. super().method(arg) more performant?

The function already has access to both __class__ and self without doing a global and local lookup:

class Demo:
    def foo(self):
        print(locals())
        print(__class__)

>>> Demo().foo()
{'self': <__main__.Demo object at 0x7fbcb0485d90>, '__class__': <class '__main__.Demo'>}
<class '__main__.Demo'>

See the source for more insight.

Overwrite answered 22/10, 2015 at 3:23 Comment(3)
also, overriding __str__() is worth mentioning, because accessing self as a value can quickly lead to infinite loop. For example, to always enclose the string, use f'"{super().__str__()}"' instead of str(super()) which will return the superclass description instead of the valueBeadsman
Hi, quick question but from testing I noticed that __getattribute__ does not handle methods like __add__, for ex. Caps('a') + 'b'. Was wondering if anyone know how to automatically handle such arithmetic methods, such that it returns a Caps instance.Budwig
@rv.kvetch - looks like you have a couple of options - your question is answered here, note that I don't vouch for the answers though #9058169Overwrite
R
30

I'm kinda horrified by the complexity of the other answers, and so is Python's standard library. You can use collections.UserString to subclass string and do not mess with proxying str's methods.

Just subclass it, and add your methods. self.data contains the actual string that is being represented by your object, so you can even implement str-"mutating" methods by reassigning self.data internally.

An example.

Rotten answered 16/6, 2018 at 21:3 Comment(3)
See also: six.moves UserString stackoverflow.com/search?q="six.moves" pythonhosted.org/six/#module-six.movesFigurehead
But UserString is not a subclass for str so that is not an option if you need isinstance checks to work.Consubstantiate
Nice answer +1. Not json serializable tho.Mottle
H
9

Here's a quick hack to do what you want: you basically intercept every function call, and, if you see that it's returning a string, you convert it back to your own class type.

While this works in this simple example, it has some limitations. Among other things, operators such as the subscript operator are apparently not handled.

class FunWrapper(object):
    def __init__(self, attr):
        self.attr = attr

    def __call__(self, *params, **args):
        ret = self.attr(*params, **args)
        if type(ret) is str:
            return Foo(ret)
        return ret

class Foo(object):
    def __init__(self, string):
        self.string = string

    def __getattr__(self, attr):
        return FunWrapper(getattr(self.string, attr))

    def newMethod(self):
        return "*%s*" % self.string.upper()


f = Foo('hello')
print f.upper().newMethod().lower()
Hedda answered 31/8, 2011 at 10:58 Comment(1)
Subscripting can be handled using __getitem__, and there are magic methods for the other operators.Gegenschein
S
0

You can try something like:

class mystr(str):
    def new_method(self):
        pass

but you won't be sure that standard methods will return a 'mystr' instance too

Stateless answered 31/8, 2011 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.