I was playing around with python and cgi and was wondering why there is a difference between the two examples below:
Example 1 (partial)
form = cgi.FieldStorage()
for field in form.keys():
sys.stdout("%s ----> %s<br />" % (field, form[field].value))
Example 2
form = {'one': '1', 'two': '2', 'three': '3'}
for field in form.keys():
sys.stdout("%s ---> %s\n" % (field, form[field]))
Why do you need the .value attribute in the first example, but in the second example you do not require it to return the key’s value?
in the first example.
form is now an object of the type
FieldStoragethis object can be accessed like a dictionary (object[key]) and it will return an object, however, unlike a normal dictionary like in example 2 which contains just strings. the object returned in example 1’s dictionary-like access must be told how you want to display it, or access it, in this case, you want the objectsvalue.to better understand this, you could try some on-the-fly debugging.
by iterating over the items in
cgi.FieldStorage()and then you can see what kind of objects they are. maybe try playing with an individual object and see how it works?incidently, if the object has a
__str__function you might not need the.valuefor more information you can read about classes and__str__