How to extend an object?

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Dec 20 20:50:45 EST 2019


On 21/12/19 1:59 am, Stefan Ram wrote:
>    I would like to add a method to a string.
> 
>    This is not possible in Python?

It's not possible. Built-in classes can't have methods added
to them.

You can define your own subclass of str and give it whatever
methods you want.

But in your case:

> for s in 'abc'.like( '(.)' ):

I would recommend making it a stand-alone function instead,
so that you would write

   for s in like('abc', '(.)'):

-- 
Greg


More information about the Python-list mailing list