Add a method to a list instance in python

前端 未结 1 1801
遥遥无期
遥遥无期 2021-02-14 21:56

I want to add a method to a single instance of the \'list\' class. Example:

a = [1,2]
a.first = lambda self: return self[0]

I know this don\'t

相关标签:
1条回答
  • 2021-02-14 22:54

    Nothing will work with a native list, since you cannot add methods to a type defined in C. You will need to derive from list and add your method to that class.

    0 讨论(0)
提交回复
热议问题