title: Python 2.7: How to build getter/setter functions to access class attributes without inheritance?
body: <p>this is probably a super nub question, apologies. I'm trying to achieve an effect in Python I am familiar with from PHP, which is building getter and setter functions for private attributes.</p>
  
  <p>In PHP I will commonly do something like this:</p>
  
  <pre><code>&lt;?php
  class newClass() {
      private $var = null;
  
      function newClass($initVal) {
          //init
      }
  
      public function get($var) {
          if ( isset($this-&gt;$var) ) {
              return $this-&gt;$var;
          }
      }
  }
  ?&gt;
  </code></pre>
  
  <p>But, trying the same idea in Python:</p>
  
  <pre><code>class newClass():
      def __init__(self, initVal):
          self.__nameOfVar1 = initVal
          self.__nameOfVar2 = initVal
  
  
      def get(self, var):
                  #assuming, when called, var = nameOfVar1
          if self.__var:
              return self.__var
  </code></pre>
  
  <p>Throws an error <code>AttributeError:'newClass' object has no attribute '__var'</code></p>
  
  <p>And quite rightly so, it doesn't. How do I build getter and setter functions to access private attributes?</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I've left the original question because it seems useful for new comers to Python to learn which conventions from other languages they might accidentally be bringing forward. But in reality, my initial problem was a bit different, in attempting to make the problem more general I obfuscated my actual need. So here's an amendment:</p>
  
  <p>In fact I was not working with private attributes; in fact, I am dealing with trying to pass an attribute downward through a composition circumstance. ie:</p>
  
  <p><code>Class A</code>, as an attribute, has a <code>Class B</code> object that does not inherit from <code>Class A</code> . It is almost never the case that <code>Class B</code> needs any information from <code>Class A</code>, but in my project there is one circumstance where, if a user chooses not provide information to <code>Class B</code>, there is a statement that makes a last-ditch attempt to infer a reasonable solution, but it requires information from <code>Class A</code> that, normally, it never needs. </p>
  
  <p>I could build <code>Class B</code> to always have had that information, but that means that almost always I'm just wasting that time and memory (trivial though it might be). To make this concrete, imagine this:</p>
  
  <p><code>toothbrush</code> has <code>bristles</code>, and <code>bristles</code> can, provided the user supplies a preferred brushing experience, create a number of bristles appropriate to either a "soft" or a "firm" brush. But, if the user doesn't provide this, then I'd like <code>bristles</code> to ask <code>brush</code> for it's surface area, allowing it to guess what an average number should be. </p>
  
  <p>I came up with this, though I have the intuitive sense that Python programmers will hate me for it; I'd just like to understand why:</p>
  
  <pre><code>@staticmethod
  def request(self, var):
      appIndex = self.__dict__
      if appIndex[var]:
          return appIndex[var]
  
  print Class.request(instance, var)
  </code></pre>
  
  <p>Works like a charm, but I think this is Python heresy. <em>Why?</em></p>