+ 1
[solved] How to make mapping object?
SoloLearn's Python Core and Python Intermediate courses do not explain anything about custom mappings. So I have a question: how to make mapping object? If needed, this is my example code: https://code.sololearn.com/c5ac65WxNnk2/?ref=app
1 Answer
+ 2
to implement Mapping behavior, you should inherit from collections.abc.Mapping and implement __iter__ (keys), __getattr__ (values) and __len__ (length):
import collections.abc
class UnpackableObject(collections.abc.Mapping):
def __iter__ (self):
for i in range(1, 5): yield i
def __getitem__(self,prop):
return 'abcd'[prop-1]
def __len__(self):
return 4
print((*UnpackableObject(),))
print({**UnpackableObject()})
Hot today
What’s wrong?
3 Votes
Question is write a c program to print prime numbers up to n and print the largest number in array.
1 Votes
Question - Java: Pyramid layers
2 Votes
Error in Program
1 Votes
What is next?
0 Votes