-2

I'm wondering if someone knows how to print a namespace that will show the key value pair of 'apple' and '$1.79'.

I understand that if I uncomment

#print(object_of_Hist_class)

I will see the data I'm asking about... but I'm curious if I can do

print(something.__dict__) 

(where, apparently, I have no idea what the something should be) to see the 'apple' and '$1.79' data. Here is the code I am talking about:

class Hist(dict):

    def __init__(self, instance_var):
         self.instance_var = instance_var

object_of_Hist_class = Hist('cha cha cha')
object_of_Hist_class['apple'] = '$1.79'

#print(object_of_Hist_class)
print(object_of_Hist_class.__dict__)
print(Hist.__dict__)
print(dict.__dict__)

ORIGINAL POST ABOVE

SOLUTION BELOW

The following is what I was looking for... First I will recreate the results I had from before with vars

print(vars(object_of_Hist_class))
print(vars(Hist))

And below is what I was looking for when I first asked this question (in particular the "data" I was hoping to find, within a dictionary displaying namespace content, can be seen to the far right of the following prints):

print(vars())
print(locals())
print(globals())

Thanks to the following post for providing this information: What's the difference between globals(), locals(), and vars()?

If you aren't in a position to run the above prints, the final three all give dictionaries with a final key value pair of: 'object_of_Hist_class': {'apple': '$1.79'}

That is what I was looking for.

I'm new to stack overflow posts (and new to things like subclassing dict, which I don't plan on doing much more of). Anyways, thanks for encouraging me to be more clear with my posts and helping me to learn more about posting procedures!

okcapp
  • 405
  • 1
  • 4
  • 15
  • 1
    Why do you want to see the keys of a dict as its attributes? You could do `dict(object_of_Hist_class, **object_of_Hist_class.__dict__)` if you want a mapping containing both. – Ashwini Chaudhary Jul 20 '17 at 07:17
  • Don't edit a question to turn it into a totally different question. – user2357112 Jul 21 '17 at 00:00
  • @user2357112 thanks for the feedback! I found the answer I was looking for so I went ahead and added it to the bottom of my original post. – okcapp Jul 21 '17 at 06:06

1 Answers1

2

It sounds like you are confusing dict items with attributes. foo.__dict__ gives you the attributes of foo. The key-value pairs in a dict are not stored as attributes, so they will not show up in its __dict__.

If you set an attribute on your object, it will show up there. For example, object_of_Hist_class.__dict__ will contain an instance_var key. Similarly, Hist.__dict__ will contain an __init__ key, along with some other special attribute names.

More information: Python 2 data model

augurar
  • 12,081
  • 6
  • 50
  • 65
  • Thanks for taking the time to reply! I came across exactly what I was looking for so I went ahead and added it to the bottom of my original post. – okcapp Jul 21 '17 at 06:15
  • @okcapp I still don't know what you're trying to accomplish, but I'm glad you found an answer that satisfies you. Instead of editing the question, you can add the information as an answer to your own question. This allows you to "accept" the answer to mark the question as solved. – augurar Jul 21 '17 at 07:57