Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subclass str in Python

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?

like image 779
yasar Avatar asked Aug 31 '11 10:08

yasar


People also ask

How do you create a subclass in Python?

The process of creating a subclass of a class is called inheritance. All the attributes and methods of superclass are inherited by its subclass also. This means that an object of a subclass can access all the attributes and methods of the superclass.

What is the use of __ str __?

Python __str__() This method returns the string representation of the object. This method is called when print() or str() function is invoked on an object. This method must return the String object.

What does __ str __ in Python?

The __str__ method in Python represents the class objects as a string – it can be used for classes. The __str__ method should be defined in a way that is easy to read and outputs all the members of the class. This method is also used as a debugging tool when the members of a class need to be checked.

What is __ subclasses __ in Python?

New-style classes i.e. subclassed from an object, which is the default in Python 3 have a __subclasses__ method. This method returns the subclasses of the class.


1 Answers

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.

like image 61
sth Avatar answered Sep 23 '22 01:09

sth