Bottom Page

Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 why is my dictionary not recognized as such
#1
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
Quote
#2
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
Quote
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#4
Thank you all
Quote
#5
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
Quote
#6
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/
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  matplotlib isn't recognized after installation Pavel_47 5 111 Sep-18-2019, 07:01 PM
Last Post: snippsat
  how do i get y to be recognized in this comprehension? Skaperen 5 177 Aug-26-2019, 07:43 PM
Last Post: Skaperen
  Class attribute not recognized\working J125 1 1,632 Dec-19-2016, 01:05 AM
Last Post: Yoriz
  Dictionary value not recognized marciokoko 1 1,405 Nov-16-2016, 04:01 PM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)