item assignment class python code example
Example: class item assignment python
class ItemAssign():
# initialize
def __init__(self, a, b):
self.a = a
self.b = b
# set item
def __setitem__(self, k, v):
if k == "a":
self.a = v
if k == "b":
self.b = v
# get item
def __getitem__(self, k):
if k == "a":
return self.a
if k == "b":
return self.b
# len
def __len__(self):
return 2
# del item
def __delitem__(self, k):
self[k] = None
# instance = ItemAssign(2, 4)
# for x in instance:
def __iter__(self):
yield self.a
yield self.b