title: How to ignore attributes that aren't defined in Python object?
body: <p>I have a Python dictionary that labels key name to attributes. The program that is tied to this dictionary is set to only have a few of the items, and only if they are necessary. So not all attributes in dictionary are defined at every pass of this script. </p>
  
  <p>Here is the code with the dictionary</p>
  
  <pre><code>def getWidths(self,sheetName):
      sheets = {
          'dclabels':self.dclabels,
          'tdclabels':self.tdclabels
      }
  
      sheetName = sheetName.lower()
      if sheetName in sheets: 
          return sheets.get(sheetName) 
      else:
          return self.colWidths
  </code></pre>
  
  <p>I am getting an error stating <code>AttributError: ClassName instance has no attribute 'dclabels'</code>
  How can I avoid this error? Is there a way I can get the script to ignore any attributes that are not defined? Thanks!</p>
  
  <p>I found a the solution to my issue.</p>
  
  <pre><code>   def getWidths(self,sheetName):
         if hasattr(self, sheetName.lower()):
             name = getattr(self,sheetName.lower())
             self.name = name
             return self.name
         else:
             return self.colWidths
  </code></pre>
  
  <p>I made use of the <code>hasattr()</code> and <code>getattr()</code> to solve my problem. Thanks to all for your suggestions.</p>