Python Forum
why is my dictionary not recognized as such
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
Reply
#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
Reply
#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
Reply
#4
Thank you all
Reply
#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
Reply
#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/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  is import cointegration_analysis a recognized module mitcht33 1 384 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  The term 'pip' is not recognized as the name of a cmdlet, function michaelnicol 1 596 Jul-16-2023, 11:12 PM
Last Post: deanhystad
  Index Function not recognized in Python 3 Peter_B_23 1 1,114 Jan-08-2023, 04:52 AM
Last Post: deanhystad
  TypeError: size; expecting a recognized type filling string dict a11_m11 0 2,486 Feb-10-2020, 08:26 AM
Last Post: a11_m11
  matplotlib isn't recognized after installation Pavel_47 5 2,755 Sep-18-2019, 07:01 PM
Last Post: snippsat
  how do i get y to be recognized in this comprehension? Skaperen 5 3,068 Aug-26-2019, 07:43 PM
Last Post: Skaperen
  pyserial-master installed bbut not recognized elwolv1 0 1,964 Jan-04-2019, 08:37 PM
Last Post: elwolv1
  How do I calculate the smallest value that is recognized as a difference when compari AFoeee 1 2,742 Oct-28-2018, 10:48 PM
Last Post: Gribouillis
  'videodigest' is not recognized as an internal or external command MM2018 2 2,728 Oct-12-2018, 02:43 PM
Last Post: MM2018
  Class attribute not recognized\working J125 1 5,292 Dec-19-2016, 01:05 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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