Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in configparser
#11
If you do it like this you are adding new_var as attribute to the dictionary.
Then you should use that attribute . access,and not ['someting'] call.
>>> config["Section"].new_var = "surname"
>>> config['Section'].new_var
'surname'

# Now dos this not work
>>> config['Section']['new_var']
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\python37\lib\configparser.py", line 1251, in __getitem__
    raise KeyError(key)
KeyError: 'new_var'
Can make both work but should not do this at all,just choice one method.
>>> config['Section']['new_var'] = 'surename'

# Now both work
>>> config['Section'].new_var
'surname'
>>> config['Section']['new_var']
'surename'
The problem now is that they are separate,changing one dos not affect the other.
>>> config['Section']['new_var'] = 'foo'
>>> config['Section']['new_var']
'foo'
>>> config['Section'].new_var
'surname'
Reply
#12
hello thanks a lot for the detailed explaination.
But i don't understand when you say "If you do it like this you are adding new_var as attribute to the dictionary."
which dictionary you are talking about?
Reply
#13
(Dec-22-2019, 07:03 PM)fullstop Wrote: which dictionary you are talking about?
The dictionary that configparser use or inheriting from internally.
If you do this you will see that it have all attributes that a dictionary has,like get,items,pop,update ...
print(dir(config['Section']))
The default call is this,so should stick with this calling method as that people expect when using configparser.
>>> config['Section']['name']
'None
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ConfigParser(dict_type=) not behaving as expected malonn 5 3,225 Sep-08-2022, 08:42 AM
Last Post: malonn
  using alternate names in configparser Skaperen 6 2,765 Oct-01-2020, 08:27 PM
Last Post: Skaperen
Question configparser.NoSectionError (Python 3.7) alpho 2 5,713 Apr-08-2020, 10:26 PM
Last Post: micseydel
  Help with ConfigParser EricALionsFan 3 3,202 Jul-27-2018, 08:46 PM
Last Post: buran
  ConfigParser.NoSectionError: No section: 'Default' degenaro 1 14,360 Jun-08-2018, 08:33 PM
Last Post: ljmetzger
  No module named 'ConfigParser' Bani 13 55,862 May-22-2017, 08:02 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020