title: Setters and Getters python
body: <p>I've looked around but everything I tried didn't seem to get any result. I just need to set a custom setter and getter for a class that converts a number to string and viceversa. Since python doesn't support which I'm using a dictionary.</p>
  
  <pre><code>class Ship(object):
      def __init__(self, type):
          self.fixed = define_direction()
  
          self.row = random_row(board, self)
          self.col = random_col(board, self)
          self.type = self.set_type(type)
          print "Type:", str(self.type)
  
      def set_type(self, type):
          return {
              0 : "Patrol boat",#2
              1 : "Destroyer",#3
              2 : "Submarine",#3
              3 : "Battleship",#4
              4 : "Aircraft carrier",#5
              }.get(type, "Patrol boat") 
      def get_type(self):
          return {
              "Patrol boat" : 0,#2
              "Destroyer" : 1,#3
              "Submarine" : 2,#3
              "Battleship" : 3,#4
              "Aircraft carrier" : 4,#5
                }.get(self.type, 0) 
      def __repr__(self):
          return "Ship. Type: ", self.get_type()
  </code></pre>
  
  <p>Not really sure that <code>self.type = self.set_type(type)</code> is legit, but it seems the only way to call a function from inside the class.</p>
  
  <p>In <code>__init__(self, type)</code> -> "type" gets passed as a number and it should get converted and stored as a string, than should get reconverted to a number while the getter is called. (maybe there's a better way to do it - using external dictionary to do the conversion and just store the number..?</p>
  
  <ul>
  <li><p>PS1: Is it legit to call an external function <code>random_row(board, self)</code> and <code>random_col</code> passing <code>self</code> even if it's not properly initialized or should I move the function inside the class as another setter?</p></li>
  <li><p>PS2: calling <code>__repl__</code>:</p>
  
  <pre><code>def __repr__(self):
    return "Ship. Type: ", str(self.get_type()), str(self.type())
  </code></pre>
  
  <p>returns:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/Users/xx/Documents/python/battleship/battleship.py", line 85, in &lt;module&gt;
      print ship.__repr__()
    File "/Users/xx/Documents/python/battleship/battleship.py", line 74, in __repr__
      return "Ship. Type: ", str(self.get_type()), str(self.type())
  TypeError: 'str' object is not callable
  </code></pre>
  
  <p>Calling it only on <code>get_type()</code></p>
  
  <pre><code>def __repr__(self):
      return "Ship. Type: ", str(self.get_type())
  </code></pre>
  
  <p>returns:</p>
  
  <pre><code>Type: Submarine
  ('Ship. Type: ', '2')
  </code></pre></li>
  </ul>
  
  <p>Hope it's clear enough.</p>