![]() |
why is my dictionary not recognized as such - 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: why is my dictionary not recognized as such (/thread-15321.html) |
why is my dictionary not recognized as such - zatlas1 - Jan-13-2019 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 RE: why is my dictionary not recognized as such - Larz60+ - Jan-13-2019 remove vars: supported_drivers = {"SQLite" : 1, "Sybase" : 3, "SQLserver" : 7, "Oracle" : 8 , "MongoDB" : 10, "DynamoDB" : 11 } print (supported_drivers)output: or as elements:for key, value in supported_drivers.items(): print('{}, {}'.format(key, value))output:
RE: why is my dictionary not recognized as such - ichabod801 - Jan-13-2019 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. RE: why is my dictionary not recognized as such - zatlas1 - Jan-13-2019 Thank you all RE: Dictionnaries - Equality - zatlas1 - Jan-13-2019 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 RE: why is my dictionary not recognized as such - woooee - Jan-14-2019 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-objects-vs-string-python/ |