in Education by
In python3 console, input those: >>> import sys >>> sys.version_info sys.version_info(major=3, minor=4, micro=3, releaselevel='final', serial=0) >>> type(sys.version_info) # this is class type >>> sys.version_info[0:2] # ?? But it acts like a list-data-like (3, 4) My questions are: How can a class act like dictionary-or-list-data-like? May give an example to construct a class like this? Is there some documentation about this? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Python contains several methods for emulating container types such as dictionaries and lists. In particular, consider the following class: class MyDict(object): def __getitem__(self, key): # Called for getting obj[key] def __setitem__(self, key, value): # Called for setting obj[key] = value If you write obj = MyDict() Then obj[3] will call the first method, and obj[3] = 'foo' will call the second method. If you further want to support len(obj) then you just need to add the method def __len__(self): # Return here the logical length Here is an example of a (very inefficient) dictionary implemented by a list class MyDict(object): def __init__(self, seq=None): self._vals = list(seq) if seq is not None else [] def __getitem__(self, key): return [v[1] for v in self._vals if v[0] == key][0] def __setitem__(self, key, val): self._vals = [v for v in self._vals if v[0] != key] self._vals.append((key, val)) def __len__(self): return len(self._vals) You can use it pretty much like a regular dict: obj = MyDict() obj[2] = 'b' >>> obj[2] 'b'

Related questions

0 votes
    In python3 console, input those: >>> import sys >>> sys.version_info sys.version_info(major=3, minor= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    In python3 console, input those: >>> import sys >>> sys.version_info sys.version_info(major=3, minor= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    In Python 2.7, I could get dictionary keys, values, or items as a list: >>> newdict = {1:0, 2:0, 3:0} ... to return a list in Python 3? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Can the Python list comprehension syntax be used to create dictionaries? For an example: By iterating over pairs of ... #doesn't work Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Which of the following list-like data structure is used for managing a dynamic collection of SparseArrays? (a) ... and answers pdf, Data Science interview questions for beginners...
asked Oct 30, 2021 in Education by JackTerrance
0 votes
    I have a dictionary d full of a collection of dataframes: key type size value gm1 dataframe mxn .. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have a file that looks like this: NODE=SC-1,CPU=90 NODE=SC-1,MEM=10 NODE=SC-1,FS=80 ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a file that looks like this: NODE=SC-1,CPU=90 NODE=SC-1,MEM=10 NODE=SC-1,FS=80 ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    How will you create a dictionary using tuples in python?...
asked Nov 24, 2020 in Technology by JackTerrance
0 votes
    How will you create a dictionary in python?...
asked Nov 24, 2020 in Technology by JackTerrance
0 votes
    The data are kept in a list whose elements are dictionaries, where each dictionary contains the data for a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I want each item to be sorted by a specific property value from a list of dictionaries I have. Take the ... When sorted by name Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    I'm developing a plugin. Take a look at the following code. string request(char post_params[]) { CURL *curl; CURLcode res; std:: ... .) { std::ofstream file ("d:/t/t.txt"); file...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    Copper behaves as a (a) Conductor always (b) Conductor or dielectric depending on the applied ... by,electromagnetic theory engineering physics,electromagnetic theory nptel...
asked Nov 6, 2021 in Education by JackTerrance
0 votes
    What is the Dictionary class in Java?...
asked May 27, 2021 in Technology by JackTerrance
...