Called to implement evaluation of self[key]. >>> ip=IP('127.0.0.0/30') >>> for x in ip: ... print repr(x) ... IP('127.0.0.0') IP('127.0.0.1') IP('127.0.0.2') IP('127.0.0.3') >>> ip[2] IP('127.0.0.2') >>> ip[-1]
(self, key)
| 530 | |
| 531 | |
| 532 | def __getitem__(self, key): |
| 533 | """Called to implement evaluation of self[key]. |
| 534 | |
| 535 | >>> ip=IP('127.0.0.0/30') |
| 536 | >>> for x in ip: |
| 537 | ... print repr(x) |
| 538 | ... |
| 539 | IP('127.0.0.0') |
| 540 | IP('127.0.0.1') |
| 541 | IP('127.0.0.2') |
| 542 | IP('127.0.0.3') |
| 543 | >>> ip[2] |
| 544 | IP('127.0.0.2') |
| 545 | >>> ip[-1] |
| 546 | IP('127.0.0.3') |
| 547 | """ |
| 548 | |
| 549 | if type(key) != types.IntType and type(key) != types.LongType: |
| 550 | raise TypeError |
| 551 | if key < 0: |
| 552 | if abs(key) <= self.len(): |
| 553 | key = self.len() - abs(key) |
| 554 | else: |
| 555 | raise IndexError |
| 556 | else: |
| 557 | if key >= self.len(): |
| 558 | raise IndexError |
| 559 | |
| 560 | return self.ip + long(key) |
| 561 | |
| 562 | |
| 563 | def __contains__(self, item): |