문제

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