Created
July 15, 2017 01:47
-
-
Save hirokiky/7bacf9aabede431e19814c93dae8d015 to your computer and use it in GitHub Desktop.
Imitiating array of PHP by Python (just a joke)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import OrderedDict | |
class PHPArray(OrderedDict): | |
def __init__(self): | |
super().__init__() | |
self.next_max_index = 0 | |
def __setitem__(self, key, val): | |
if isinstance(key, int): | |
if self.next_max_index <= key: | |
self.next_max_index = key + 1 | |
super().__setitem__(key, val) | |
def __iter__(self): | |
return self.values() | |
def push(self, val): | |
self[self.next_max_index] = val | |
a = PHPArray() | |
a.push('a') | |
print("push('a')") | |
print(a) | |
a['ky'] = 'hiroki' | |
print("a['ky'] = 'hiroki'") | |
print(a) | |
a.push('dag') | |
print("a.push('dag')") | |
print(a) | |
a[99999] = 'shmizukawa' | |
print("a[99999] = 'shmizukawa'") | |
print(a) | |
a.push('terapyon') | |
print("a.push('terapyon')") | |
print(a) | |
a[9] = 'shmizukawa' | |
print("a[9] = 'shmizukawa'") | |
print(a) | |
a.push('terapyonpyon') | |
print("a.push('terapyonpyon')") | |
print(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment