title: How does Base.metadata work in Sqlalchemy?
body: <p>Code/Example</p>
  
  <pre><code>from sqlalchemy.ext.declarative import declarative_base
  
  Base = declarative_base()
  </code></pre>
  
  <p>If I run <code>print(Base)</code> then I get result:</p>
  
  <pre><code>&lt;class 'sqlalchemy.ext.declarative.api.Base'&gt;
  </code></pre>
  
  <p>(I.e <code>Base</code> is a class created by the function <code>declarative_base</code>).</p>
  
  <p>If I run <code>print(Base.metadata)</code> then I get </p>
  
  <pre><code>Metadata(bind=engine(postgres//:user:password@host/database))
  </code></pre>
  
  <p>But exactly how does <code>print(Base.metadata)</code> even run if <code>Base</code> is a class and <code>metadata</code> is an object?</p>
  
  <p>How are the two connected?</p>
  
  <p>I could understand if metadata was a class attribute but from what I understand it is not so how does this work?</p>
  
  <p>Is there something in python syntax that I have missed?</p>
  
  <p>Looking in metadata code:</p>
  
  <pre><code>from sqlalchemy import MetaData
  metadata = MetaData()
  </code></pre>
  
  <p>What is here the definition of <code>metadata</code> and <code>MetaData</code>?
  Are they both objects?
  Is <code>MetaData()</code> a function or something else? </p>
  
  <p>According to SQLAlchemy documentation <code>MetaData</code> is: </p>
  
  <blockquote>
    <p>a container object that keeps together many different features of a database (or multiple databases) being described.</p>
  </blockquote>
  
  <p>But if it is an object so then how can it be run as something similar to a function: <code>Metadata()</code>? </p>
  
  <h2>Summary:</h2>
  
  <ul>
  <li>How are <code>Base</code> and <code>metadata</code> connected if one is a class and the other is an object?</li>
  <li>How can the command (<code>Base.metadata</code>) be run?</li>
  <li>What is the difference between <code>metadata</code> and <code>MetaData</code> and what is the <code>MetaData()</code> definition?</li>
  </ul>