2

I have a dictionary with objects being stored as values. How might I access and print the attributes of a particular object? For example,

dict={0:obj0, 1:obj1, 2:obj2, 3:obj3}

I want to print obj1.attribute. I have tried:

print (dict[key]).attribute

to no avail. I cannot just access the object directly because I have not assigned it to a variable. This program automatically generates objects and places them in dict with an automatically generated key, which spares me manual assignment of an arbitrary amount of values. If anyone knows a better way to phrase this question, please go ahead. Thank you so much!

EDIT: My dictionary's name is not 'dict', nor is my attribute named 'attribute.' These are used simply for clarity.

EDIT: OK, here's what's going on. I'm using Tkinter to retrieve contact information through entry fields. I'm using that input to create an object with attributes name, address, etc. So,

class User():

    def __init__(self, name='', street='', city='', state='', zip=''):
        self.name=name
        self.street=street
        self.city=city
        self.state=state
        self.zip=zip

###### code below is outside of class User #####

    def make(name='', street='', city='', state='', zip='', count=0):
        dic[count]=User(name='', street='', city='', state='', zip='')

Is there a reason that "dic[0].name" would return an empty string?

9
  • You use getattr: getattr(d[key], attribute_name). Commented Feb 7, 2014 at 4:33
  • That doesn't work, unfortunately. Commented Feb 7, 2014 at 4:54
  • 1
    Can you post a functional example of your problem? I can't understand your question with just the code that you've provided. Commented Feb 7, 2014 at 5:00
  • The original question is updated, thank you for your help. Commented Feb 7, 2014 at 5:05
  • 1
    @Johnny_Rose you're setting name to an empty string. Commented Feb 7, 2014 at 5:05

1 Answer 1

1

Change this

def make(name='', street='', city='', state='', zip='', count=0):
    dic[count]=User(name='', street='', city='', state='', zip='')

to

def make(name='', street='', city='', state='', zip='', count=0):
    dic[count]=User(name=name, street=street, city=city, state=state, zip=zip)

In case your attribute is containing whitespaces or an empty string, you can do that in some case, it will help you know if something is there.

print "<%s>" % my_dict[key].attribute

If you see <> it's likely that this is an empty string, if you see something else in between, it could be spaces are tabs or newlines or anything that isn't visible.

The other possibility is that for some reasons, the attribute you're getting is an object that override __repr__ or __str__ and returns an empty string. Without knowing with what you're dealing with, it is very hard to help more than that.

Sign up to request clarification or add additional context in comments.

7 Comments

My objects are not saved as dicts, they are saved as the value of a key. So my key might be x, and the value is myObject. I know what attributes are present, I just can't print them. It won't create an error, it will just be blank. If I tried to print "Hello, " myDict[key].attribute and it just prints "Hello, ".
@Johnny_Rose what does type(my_dict[key].attribute) yield? Also if you look into dir(my_dict[key]) is the attribute you need there? Have you considered that the attribute might be returning an empty string?
type(my_dict[key].attribute) returns string. I have considered the empty string, but the attribute in question is "name", and I know for certain there is a string of characters in it.
@Johnny_Rose How do you know it is there? It obviously is not. Show the whole code you are using (trim out things not needed). Otherwise, this is non-reproducible.
@Johnny_Rose if you can provide an example of the object it would be a better idea, the whole dict thing is probably not the issue in that case.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.