Implementing a custom string method

后端 未结 3 1317
余生分开走
余生分开走 2021-01-17 23:15

How do you add a custom method to a built-in python datatype? For example, I\'d like to implement one of the solutions from this question but be able to call it as follows:

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-18 00:01

    You can't. And you don't need to.

    See Extending builtin classes in python for an alternative solution. Subclassing is the way to go here.

提交回复
热议问题