I'm just curious is it possible to create function in Python which work on any sequence. For example:

>>> def head(self):
>>>     return self[0]
>>>
>>> a = [1, 2, 3]
>>> a.head()
1
>>> xrange(10).head()
0
有帮助吗?

解决方案

You cannot extend the built-in types, no.

You can only subclass them, but that only helps if you have full control over the creating and use.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top