class item assignment python

Solutions on MaxInterview for class item assignment python by the best coders in the world

showing results for - "class item assignment python"
Princeton
21 Jan 2020
1class ItemAssign():
2# initialize
3    def __init__(self, a, b):
4        self.a = a
5        self.b = b
6# set item
7    def __setitem__(self, k, v):
8        if k == "a":
9            self.a = v
10        if k == "b":
11            self.b = v
12# get item
13    def __getitem__(self, k):
14        if k == "a":
15            return self.a
16        if k == "b":
17            return self.b
18# len
19    def __len__(self):
20        return 2
21# del item
22    def __delitem__(self, k):
23        self[k] = None
24# instance = ItemAssign(2, 4)
25# for x in instance:
26    def __iter__(self):
27        yield self.a
28        yield self.b