-1

For example if I were using a list i would say

for i in taskList:
   Print('item')

I was wondering how to do this with a dict also this dict is in another class. I have a class set up for the user to input tasks which adds it to a dict (this works) however now I want another class that pulls from the dict in the first class. is this possible? Here is a short example of what I've tried

class TaskList():
   itemsToDoDic = {'Cleaning' : 0, 'Cook' :0}


classItems = TaskList()

class Importance():
   classItems.itemsToDoDic
   def levelOfImportance(self):
      for i in self.ClassItems.itemsToDoDic:
         importanceValue = int(input('Enter a 
         number 1 - 5')

I got the following errors:

  1. Instance of 'Importance' has no 'ClassItems' member
  2. Unused variable 'i'
  3. Unused variable 'importanceValue'
quamrana
  • 37,849
  • 12
  • 53
  • 71
SpacedOutKID
  • 95
  • 1
  • 14

2 Answers2

1

Say you have two classes, needless to say, that if your source class (the one you get the dict from) is in another file, you will have to import it.

Check this code:

# this is the source class
class DictContainingClass():
  itemsToDoDic = {'Cleaning' : 0, 'Cook' :0}

# creating an instance of the source class
instance = DictContainingClass()

# this is the taking class
class DictNeedingClass():

  
  # creating your class method:
  def myClassMethod(self):

    # accessing the dict inside it
    getDict = instance.itemsToDoDic
  
    # now looping through its items
    for item in getDict.items():
      print(item)
    # or if you want to access only the keys
    for key in getDict.keys():
      print(key)
    # or if you want to access only values
    for value in getDict.values():
      print(value)

# calling for your method
DictNeedingClass().myClassMethod()

Hope this answered your question, please refer to this dictionary methods page for more information.

  • 1
    Thank you so much! This actually helped and the code works now! I honestly have been stuck on this for awhile. Appreciate you taking the time out of your day to help solve this and reply fast, means a lot! – SpacedOutKID Jul 21 '20 at 12:57
  • @SpacedOutKID This is my first time I contribute to the community, I myself am new to coding. This is encouraging, good luck on your journey. Please consider checking this thread as answered if your problem is solved. –  Jul 21 '20 at 13:24
0

Here is the solution to your error.

Here in for loop for i in self.ClassItems.itemsToDoDic: you are calling self.ClassItems.itemsToDoDic which is not defined in Importance class rather defined as a global variable.

class TaskList():
   itemsToDoDic = {'Cleaning' : 0, 'Cook' :0}

classItems = TaskList()

class Importance():
    def levelOfImportance(self):
        for key,value in classItems.itemsToDoDic.items():
            print(key,value)
            #Rest of your code here

#testing
Importance().levelOfImportance() 

Check this page if you want to know more about for loop over the dictionary variable. https://www.geeksforgeeks.org/iterate-over-a-dictionary-in-python/

Bipin Maharjan
  • 423
  • 6
  • 13