Python Forum

Full Version: why is my dictionary not recognized as such
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am still a newbie but learning fast. However, Python is still surprising me in strange ways. Here is my code
supported_drivers = {"SQLite" : 1,
                    "Sybase" : 3,
                    "SQLserver" : 7,
                    "Oracle" : 8 ,
                    "MongoDB" : 10,
                    "DynamoDB" : 11
                    }
print (vars(supported_drivers))
and here is the result:
Traceback (most recent call last):
File "Z:\projects\xxx\yyy.py", line 58, in <module>
print (vars(supported_drivers))
TypeError: vars() argument must have __dict__ attribute.

OK, so why is my, seemingly a dictionary, not a dictionary?

Thank you all
ZA
remove vars:
supported_drivers = {"SQLite" : 1,
                    "Sybase" : 3,
                    "SQLserver" : 7,
                    "Oracle" : 8 ,
                    "MongoDB" : 10,
                    "DynamoDB" : 11
                    }
print (supported_drivers)
output:
Output:
{'SQLite': 1, 'Sybase': 3, 'SQLserver': 7, 'Oracle': 8, 'MongoDB': 10, 'DynamoDB': 11}
or as elements:
for key, value in supported_drivers.items():
    print('{}, {}'.format(key, value))
output:
Output:
SQLite, 1 Sybase, 3 SQLserver, 7 Oracle, 8 MongoDB, 10 DynamoDB, 11
Because vars isn't looking for a dictionary, it's looking for something with a __dict__ attribute, which dictionaries don't have. The vars function is for things like classes and instances.
Thank you all
I must admit that Python comparisons and matches begin to drive me nuts:
       self.driver_id = supported_drivers[self.driver] if self.driver in supported_drivers else None
        print (self.driver, "\n", supported_drivers)

        print (self.driver, "  ", self.dbengine, "  ", self.driver_id, "\n\n")
        if 'Sybase' == self.driver:
            print ('Sybase', ' is equal ', self.driver)
            if self.dbengine == 'ASE':
                self.driver_id = 4
            elif self.dbengine == 'IQ':
                self.driver_id = 5
            elif self.dbengine != '':
                print(self.driver, "\n\n")
                sys.exit(self.dbengine)
        else:
            print ('Sybase', ' is not equal ', self.driver)
results:

'Sybase'
{'SQLite': 1, 'Sybase': 3, 'SybaseASE': 4, 'SybaseIQ': 5, 'SQLserver': 7, 'Oracle': 8, 'MongoDB': 10, 'DynamoDB': 11}
'Sybase' 'BLA' None


Sybase is not equal 'Sybase'

-------
as you see, self.driver is 'Sybase' and it is IN supported_drivers, yet Python says it is NOT.
And then it is NOT EQUAL to 'Sybase'...
What the heck is going here?

Now I do compare strings unless you tell me that in Python a string is also not a string under certain circumstances

Thank you
ZA
Print the type of "Sybase" and self.driver. You may be comparing one type to another which is never equal. https://www.geeksforgeeks.org/byte-objec...ng-python/