title: how to access a class attribute from within the class in Python
body: <p>Consider the following SQLAlchemy code</p>
  
  <pre><code>class Foo(Base):
      __tablename__ = 'currency'
  
      id = Column(Integer, primary_key=True)
      name = Column(String(40), nullable=False, unique=True)
      symbol = Column(String(4), nullable=False, unique=True)
  
      l =  session.query(Foo.symbol, Foo.id).all()
  </code></pre>
  
  <p>In the last line, I am trying to generate a list of the symbol-id pairs. It produces the following error:  </p>
  
  <blockquote>
    <p>NameError: name 'Foo' is not defined</p>
  </blockquote>
  
  <p>I tried the following and got the errors as specified:</p>
  
  <pre><code>l = session.query(models.Foo.symbol, models.Foo.id).all()
  #note: The Foo object is in the models.py file
  #error: NameError: name 'models' is not defined
  
  l = session.query(symbol, id).all()
  #error: sqlalchemy.exc.CompileError: Cannot compile Column object until its 'name' is assigned.
  
  l = session.query(self.symbol, self.id).all()
  #error: NameError: name 'self' is not defined
  </code></pre>
  
  <p>So how do I pass in Foo object's column names to the SqlAlhemy query from inside the Foo class?</p>
  
  <p>Why am I doing this? I am then converting the list to a dictionary and only access the dictionary from code throughout the program since its values are seldom changed. So I want to populate it once and then access it many many times without hitting the DB again. And I want to keep it in the Foo Class which I believe is where it belongs.</p>
  
  <p>If there is a better way to do this please do let me know.</p>