assuming you want to extend the str class by introducing a new hellow_world () method, you can do it like this
>>> class my_string(str): ... def hello_world(self): ... print "Hello World" ... >>> s = my_string() >>> s.hello_world() Hello World
If in a newly introduced method you need to use methods from the parent class, for example, you want to define a double_count method that will return twice the number of a given character, you can do
>>> class my_string(str): ... def double_count(self, c): ... return super(my_string, self).count(c) * 2 >>> s = my_string("abcd") >>> print s.double_count("a") 2
Hope you find this post helpful.
Nassim Seghir Aug 15 '18 at 17:56 2018-08-15 17:56
source share