Python: from a dict, how retrieve object as key

If I have a dictionary of several Object:value,, How can I retrieve certain Object using it as [key]?

For example

class Obj():
    def __init__(self, value):
        self.value = value

dct = {Obj(foo):foo_value, Obj(bar):bar_value}

#How to do something like
#>>> dct[foo]
#foo_value

Suppose that foo_value can't be aasigned as property of Obj.

So far, this is what I get (abstracted)

class Obj():
    def __init__(self, name):
        self.name = name
    def __hash__(self):
        return hash(tuple(sorted(self.__dict__.items())))
    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self.__dict__ == other.__dict__
        else:
            return False
    def __repr__(self):
        return str(self.name)

dct = {Obj('item1'):1, Obj('item2'):2}

print(dct.keys())
dct['item1']

And the output

dict_keys([item1, item2])
Traceback (most recent call last):
    File "C:\Users\ivan\Desktop\multi_e.py", line 197, in <module>
     dct['item1']
KeyError: 'item1'


Asked by: Clark650 | Posted: 27-01-2022






Answer 1

What about using a custom implementation of dict?

class FieldDict(dict):
    def __getitem__(self, item):
        return dict.__getitem__(self, Obj(item))

dct = FieldDict({Obj('item1'):1, Obj('item2'):2})

print(dct.keys())
print(dct['item1'])  # prints 1

Answered by: Emily868 | Posted: 28-02-2022



Answer 2

That will not work as the keys are not the strings, but they are objects of type Obj. Even though all the objects hold that string You could do this. Store variables as references to objects as keys

x = Obj("item1")
y = Obj("item2")
dct= {x:1, y:2}

And to retrieve you need to do:

>>> dct[x]
1

Answered by: Fenton796 | Posted: 28-02-2022



Answer 3

You could roll-out your own dict subclass with custom __getitem__ and __setitem__ and you really don't need any complicated __hash__ method on your Obj class after this, just __init__ will suffice.

class Obj:
    def __init__(self, value):
        self.name = value

class MyDict(dict):
    def __setitem__(self, key, value):
        if isinstance(key, Obj):
            dict.__setitem__(self, key.name, value)
        else:
            dict.__setitem__(self, key, value)

    def __getitem__(self, key):
        if isinstance(key, Obj):
            return dict.__getitem__(self, key.name)
        return dict.__getitem__(self, key)

Demo:

>>> dct = MyDict()
>>> dct[Obj('item1')] = 1
>>> dct[Obj('item2')] = 2
>>> dct
{'item1': 1, 'item2': 2}    
>>> dct['item1']
1
>>> dct[Obj('item1')]
1

Answered by: Grace460 | Posted: 28-02-2022



Answer 4

I was a trying for a little bit time, but I think I got what you want, look:

edit thanks to @user2357112

class Obj():
    def __init__(self, value):
        self.value = value

    def __eq__(self, other):
      """Override the default Equals behavior"""
      if isinstance(other, self.__class__):
          return self.value == other.value
      return False

    def __ne__(self, other):
      """Define a non-equality test"""
      return not self.__eq__(other)


    def __hash__(self):
      return id(self.value)

class Custom_dict(dict):

    def __getitem__(self, item):
      return dict.__getitem__(self, Obj(item))

x = Custom_dict()
x[Obj('asdf')] = 5
print(x['asdf'])


dct = Custom_dict({Obj('item1'):1, Obj('item2'):2})
print([key.value for key in dct.keys()])
print(dct['item1'])    

5 ['item1', 'item2']

1

Answered by: John444 | Posted: 28-02-2022



Similar questions

sql server - Python: Retrieve Image from MSSQL

I'm working on a Python project that retrieves an image from MSSQL. My code is able to retrieve the images successfully but with a fixed size of 63KB. if the image is greater than that size, it just brings the first 63KB from the image! The following is my code: #!/usr/bin/python import _mssql mssql=_mssql.connect('&lt;ServerIP&gt;','&lt;UserID&gt;','&lt;Password&gt;') mssql.select_db('&lt;Database...


Python: Retrieve items from a set

In general, Python sets don't seem to be designed for retrieving items by key. That's obviously what dictionaries are for. But is there anyway that, given a key, you can retrieve an instance from a set which is equal to the key? Again, I know this is exactly what dictionaries are for, but as far as I can see, there are legitimate reasons to want to do this with a set. Suppose you have a class defined something l...


tuple in redis / python: can store, not retrieve

So, I've got redis working with python -- exciting! I need to store a tuple and retrieve it / parse it later. Construct below isn't working, I think because the returned tuple is quoted -- there is a quote on either end of it. It seems to me that the quotes indicate that it isn't actually a tuple, but rather a string. So does anyone know how to get redis to actually return a working tuple? Thank...


numpy - Python: retrieve values after index in each column of table

I would like to get a table with values after each cell = 100 in a table. Is there an efficient method for completing this? Now: Col1 Col2 Col3 Col4 1 89 100 92 2 100 14 88 3 75 18 100 4 34 56 63 To: Col1 Col2 Col3 Col4 1 nan 100 nan 2 100 14 nan 3 75 18 100 4 34 56 63 I've tried: f...


Python: How to retrieve second word from the text

So the generator function generates a word char by char until "" and now I want the main function to call out generator function 100 times so that it would create a list words with 100 words. As I have it now it will call out the function 100x but only with one word. What should I do so that it would remember the words it has used already. word = " " def generator(): global word with open("text.txt...


sqlite - Python: retrieve number of rows affected with SQL DELETE query

It seems it is quite easy to retrieve the number of rows SELECTed with a SQL query with cursor.execute("SELECT COUNT(*) from ...") result=cursor.fetchone() but how should I retrieve the number of rows by a DELETE query?


Python: How to retrieve the count of values in a column reading a CSV?

Closed. This question needs to be more focused. It ...


Python: Retrieve list of sockets on current machine?

I'm brand new to Python (as of last week) and I'm still getting to grips with the basics so please excuse any ignorance I display. As part of my homework I have been asked to make a basic port scanner and one of the functions I have to include is the retrieval of a list of sockets on the current machine. I have been looking around and managed to piece together a piece of code that allows me to enter the IP of the m...


Python: XML retrieve from a URL to CSV

I am trying to write a Python script that dynamically reads the XML data from a URL, (e.g. http://www.wrh.noaa.gov/mesowest/getobextXml.php?sid=KCQT&amp;num=72) The format of the XML is as follows: &lt;station id="KCQT" name="Los Angeles / USC Campus Downtown" elev="179" lat="34.02355" lon="-...


Python: Retrieve Tuples From Set Based on First Value of Tuple

Suppose I have a set, s that looks like this: s = set([(1,2), (1,4), (2,6)]) I want to retrieve all tuples in my set that have first element 1. Usually I'd have to give a full tuple, something like: (1,2) in s In this case, I want to retrieve all tuples of the form (1,_) where _ can be any number.


html - How can I retrieve the page title of a webpage using Python?

How can I retrieve the page title of a webpage (title html tag) using Python?


python - How to retrieve an element from a set without removing it?

Suppose the following: &gt;&gt;&gt; s = set([1, 2, 3]) How do I get a value (any value) out of s without doing s.pop()? I want to leave the item in the set until I am sure I can remove it - something I can only be sure of after an asynchronous call to another host. Quick and dirty: &gt;&gt;&gt; elem = s.pop() &gt;&gt;&gt; s.add(elem)


sql server - Python: Retrieve Image from MSSQL

I'm working on a Python project that retrieves an image from MSSQL. My code is able to retrieve the images successfully but with a fixed size of 63KB. if the image is greater than that size, it just brings the first 63KB from the image! The following is my code: #!/usr/bin/python import _mssql mssql=_mssql.connect('&lt;ServerIP&gt;','&lt;UserID&gt;','&lt;Password&gt;') mssql.select_db('&lt;Database...


python - Best way to retrieve variable values from a text file?

Referring on this question, I have a similar -but not the same- problem.. On my way, I'll have some text file, structured like: var_a: 'home' var_b: 'car' var_c: 15.5 And I need that python read the file and then create a variable named var_a with value 'home', and so on. Example...


python - How to retrieve the selected text from the active window

I am trying to create a simple open source utility for windows using Python that can perform user-defined actions on the selected text of the currently active window. The utility should be activated using a pre-defined keyboard shortcut. Usage is partially outlined in the following example: The user selects some text using the mouse or the keyboard (in any application window)


python - How can I retrieve last x elements in Django

I am trying to retrieve the latest 5 posts (by post time) In the views.py, if I try blog_post_list = blogPosts.objects.all()[:5] It retreives the first 5 elements of the blogPosts objects, how can I reverse this to retreive the latest ones? Cheers


python - Retrieve module object from stack frame

Given a frame object, I need to get the corresponding module object. In other words, implement callers_module so this works: import sys from some_other_module import callers_module assert sys.modules[__name__] is callers_module() (That would be equivalent because I can generate a stack trace in the function for this test case. The imports are there simply to make that example complete an...


How do I retrieve Hotmail contacts with python

How can I retrieve contacts from hotmail with python? Is there any example?


linux - How to retrieve the process start time (or uptime) in python

How to retrieve the process start time (or uptime) in python in Linux? I only know, I can call "ps -p my_process_id -f" and then parse the output. But it is not cool.


python - Retrieve the two highest item from a list containing 100,000 integers

How can retrieve the two highest item from a list containing 100,000 integers without having to sort the entire list first?






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top