Python Forum
AttributeError: 'dict' object has no attribute 'fees' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: AttributeError: 'dict' object has no attribute 'fees' (/thread-9627.html)



AttributeError: 'dict' object has no attribute 'fees' - mattraffel - Apr-19-2018

I do not understand how to allocate a python dictionary. I was hoping someone could point out my error. If this is not the right forum, please help me out and point me to the right one.

This is my code:

msg = {'fees': ['GMBk8YVHn2', 2, 'C4xbW4SR'] }
fees = getattr(msg, "fees")
Traceback (most recent call last): File "<input>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'fees'
What's wrong with msg data?

Thnx
Matt


RE: AttributeError: 'dict' object has no attribute 'fees' - Larz60+ - Apr-19-2018

The format that you use is indeed a dictionary.
It contains one item whos'e key is 'fees', and who's value is a list containing three items.
so to get fees, you would use:
fees = msg['fees']
or to access individual list item (say 2nd one)
second = msg['fees'][1]
>>> msg = {'fees': ['GMBk8YVHn2', 2, 'C4xbW4SR'] }
>>> fees = msg['fees']
>>> fees
['GMBk8YVHn2', 2, 'C4xbW4SR']
>>> second = msg['fees'][1]
>>> second
2
>>>
You can programmatically add a new item to the dictionary like:
>>> msg['taxes'] = ['A', 'Sales', 'whatever']
>>> msg
{'fees': ['GMBk8YVHn2', 2, 'C4xbW4SR'], 'taxes': ['A', 'Sales', 'whatever']}
>>> msg['taxes'][1]
'Sales'
>>>



RE: AttributeError: 'dict' object has no attribute 'fees' - mattraffel - Apr-19-2018

Interesting: why does the getattrib request fail then? am I using it wrong?

fees = getattr(msg, "fees")
Thnx
Matt


RE: AttributeError: 'dict' object has no attribute 'fees' - Gribouillis - Apr-19-2018

(Apr-19-2018, 09:04 PM)mattraffel Wrote: Interesting: why does the getattrib request fail then? am I using it wrong?
getattr retrieves objects' attributes values, not dictionary values. You can use
fees = msg.get("fees")



RE: AttributeError: 'dict' object has no attribute 'fees' - mattraffel - Apr-19-2018

Can you give me an example of how the data should be structured so that fees is an attribute?

Thnx
Matt


RE: AttributeError: 'dict' object has no attribute 'fees' - Gribouillis - Apr-19-2018

(Apr-19-2018, 09:13 PM)mattraffel Wrote: Can you give me an example
class Foo:
    pass

foo = Foo()
foo.fees = ['GMBk8YVHn2', 2, 'C4xbW4SR']
print(getattr(foo, 'fees'))



RE: AttributeError: 'dict' object has no attribute 'fees' - mattraffel - Apr-20-2018

(Apr-19-2018, 09:18 PM)Gribouillis Wrote:
(Apr-19-2018, 09:13 PM)mattraffel Wrote: Can you give me an example
class Foo:
    pass

foo = Foo()
foo.fees = ['GMBk8YVHn2', 2, 'C4xbW4SR']
print(getattr(foo, 'fees'))

Thank you.

I also found a slightly different way. It produces the same results. I thought I would post it in case it helps someone else.

class Foo:
    pass

foo = Foo()
setattr(foo, "fees", ['GMBk8YVHn2', 2, 'C4xbW4SR'])
print(getattr(foo, 'fees'))