Add a method to a list instance in python

前端 未结 1 1314
失恋的感觉
失恋的感觉 2021-02-14 21:53

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:47

    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)
提交回复
热议问题