*Memos:
- My post explains the custom list and tuple with __getitem__() and __setitem__().
- My post explains the custom set, dictionary and string with __getitem__() and __setitem__().
__getitem__() is the special(magic) method which can make its class object subscriptable and iterable to get a value as shown below:
*Memos:
- The 1st parameter is
self
(Required). *self
can be other name. - The 2nd parameter and 1st argument are
key
(Required): *Memos:- It can be any types.
-
key
can be other name.
class MyCls: def __getitem__(self, key): return key v = MyCls() print(v) # <__main__.MyCls object at 0x0000021AF5C71640> print(v[0], v[1], v[2], v[3], v[4]) print(v.__getitem__(key=0), v.__getitem__(key=1), v.__getitem__(key=2), v.__getitem__(key=3), v.__getitem__(key=4)) # 0 1 2 3 4 print(v[1:3]) print(v.__getitem__(key=slice(1, 3))) # slice(1, 3, None) print(v[:]) print(v.__getitem__(key=slice(None))) # slice(None, None, None) print(v[2.3], v[2.3+4.5j], v[True], v['a'], v[None]) print(v.__getitem__(key=2.3), v.__getitem__(key=2.3+4.5j), v.__getitem__(key=True), v.__getitem__(key='a'), v.__getitem__(key=None)) # 2.3 (2.3+4.5j) True a None print(v[[2, 3]], v[(2, 3)], v[{2, 3}], v[{'a':'b'}]) print(v.__getitem__(key=[2, 3]), v.__getitem__(key=(2, 3)), v.__getitem__(key={2, 3}), v.__getitem__(key={'a':'b'})) # [2, 3] (2, 3) {2, 3} {'a': 'b'} for x in v: # It breaks when x is 5 if x == 5: # otherwise infinity loop occurs. break print(x) # 0 # 1 # 2 # 3 # 4 v[0] = 'a' v[1:3] = ['a', 'b', 'c', 'd', 'e'] # TypeError: 'MyCls' object does not support item assignment v.__setitem__(key=0, value='a') v.__setitem__(key=slice(1, 3), value=['a', 'b', 'c', 'd', 'e']) # AttributeError: 'MyCls' object has no attribute '__setitem__'
__setitem__() is the special(magic) method which can make its class object assignable to set a value as shown below:
*Memos:
- The 1st parameter is
self
(Required). *self
can be other name. - The 2nd parameter and 1st argument are
key
(Required): *Memos:- It can be any types.
-
key
can be other name.
- The 3rd parameter and 2nd argument are
value
(Required): *Memos:- It can be any types.
-
value
can be other name.
class MyCls: def __setitem__(self, key, value): print(key, value) v = MyCls() print(v) # <__main__.MyCls object at 0x0000025540688950> v[0] = 'a' v.__setitem__(key=0, value='a') # 0 a v[1] = 'b' v.__setitem__(key=1, value='b') # 1 b v[2] = 'c' v.__setitem__(key=2, value='c') # 2 c v[3] = 'd' v.__setitem__(key=3, value='d') # 3 d v[4] = 'e' v.__setitem__(key=4, value='e') # 4 e v[1:3] = ['a', 'b', 'c', 'd', 'e'] v.__setitem__(key=slice(1, 3), value=['a', 'b', 'c', 'd', 'e']) # slice(1, 3, None) ['a', 'b', 'c', 'd', 'e'] v[:] = ['a', 'b', 'c', 'd', 'e'] v.__setitem__(key=slice(None), value=['a', 'b', 'c', 'd', 'e']) # slice(None, None, None) ['a', 'b', 'c', 'd', 'e'] v[2.3] = 4.5 v.__setitem__(key=2.3, value=4.5) # 2.3 4.5 v[2.3+4.5j] = 6.7+8.9j v.__setitem__(key=2.3+4.5j, value=6.7+8.9j) # (2.3+4.5j) (6.7+8.9j) v[True] = False v.__setitem__(key=True, value=False) # True False v['a'] = 'b' v.__setitem__(key='a', value='b') # a b v[None] = None v.__setitem__(key=None, value=None) # None None v[[2, 3]] = [4, 5] v.__setitem__(key=[2, 3], value=[4, 5]) # [2, 3] [4, 5] v[(2, 3)] = (4, 5) v.__setitem__(key=(2, 3), value=(4, 5)) # (2, 3) (4, 5) v[{2, 3}] = {4, 5} v.__setitem__(key={2, 3}, value={4, 5}) # {2, 3} {4, 5} v[{'a':'b'}] = {'c':'d'} v.__setitem__(key={'a':'b'}, value={'c', 'd'}) # {'a': 'b'} {'c': 'd'} print(v[0], v[1], v[2], v[3], v[4]) print(v[1:3]) # TypeError: 'MyCls' object is not subscriptable print(v.__getitem__(key=0), v.__getitem__(key=1), v.__getitem__(key=2), v.__getitem__(key=3), v.__getitem__(key=4)) print(v.__getitem__(key=slice(1, 3))) # AttributeError: 'MyCls' object has no attribute '__getitem__' for x in v: # It breaks when x is 5 if x == 5: # otherwise infinity loop occurs. break print(x) # TypeError: 'MyCls' object is not iterable
Top comments (0)