title: How to write a Python rectangle class?
body: <p>So I am trying to do this:</p>
  
  <p>Create a class named <code>Rectangle</code> with the following attributes and methods (sample run for each method included): </p>
  
  <ol>
  <li><p>Each instance should have an x, y, width, and height attributes.</p></li>
  <li><p>You should be able to pass the attributes when creating the rectangle as follows, (where x=5, y=10, width=50, height=100 in that order):</p>
  
  <pre><code>r = Rectangle(5, 10, 50, 100)        
  </code></pre></li>
  <li><p>Create a method that returns the rectangle as a string (hint: implement <strong>str</strong> ). For a rectangle object with attribute values x=5, y=10, width=50, height=100, it should return the string <code>Rectangle(5, 10, 50, 100)</code>.</p>
  
  <pre><code>&gt;&gt;&gt; r2 = Rectangle(5, 10, 50, 100)
  &gt;&gt;&gt; print(r2)
    Rectangle(5, 10, 50, 100)      
  </code></pre></li>
  <li><p>Create a method called <code>right</code> that gets the value of the right edge of the rectangle. It should take no arguments:   </p>
  
  <pre><code>&gt;&gt;&gt; r3 = Rectangle(3, 5, 10, 20)
  &gt;&gt;&gt; r3.right()
    13     
  
  &gt;&gt;&gt; r4 = Rectangle(12, 10, 72, 35)
  &gt;&gt;&gt; r4.right()
    84 
  </code></pre></li>
  <li><p>Create a method called <code>bottom</code> that gets the value of the bottom edge of the rectangle.  </p>
  
  <pre><code>&gt;&gt;&gt; r5 = Rectangle(5, 7, 10, 6)
  &gt;&gt;&gt; r5.bottom()
    13      
  
  &gt;&gt;&gt; r5.y += 12    
  &gt;&gt;&gt; r5.bottom()
    25         
  </code></pre></li>
  <li><p>Create a method called <code>size</code> that returns the width and height of your rectangle.      </p>
  
  <pre><code>&gt;&gt;&gt; r6 = Rectangle(1, 2, 3, 4)
  &gt;&gt;&gt; r6.size()
    (3, 4) 
  </code></pre></li>
  <li><p>Create a method called <code>position</code> that returns the x and y coordinates of your rectangle.</p>
  
  <pre><code>&gt;&gt;&gt; r6.position()
    (1, 2) 
  </code></pre></li>
  <li><p>Create a method called <code>area</code> that returns the area of your rectangle.</p>
  
  <pre><code>&gt;&gt;&gt; r6.area()
    12     
  </code></pre></li>
  <li><p>Create a method called <code>expand</code> that takes an offset value and returns a copy of the rectangle expanded with offset in all directions.</p>
  
  <pre><code>&gt;&gt;&gt; r = Rectangle(30, 40, 100, 110)
  &gt;&gt;&gt; r    
    Rectangle(30, 40, 100, 110)
  
  &gt;&gt;&gt; r1 = r.expand(offset=3) 
  &gt;&gt;&gt; r1    
    Rectangle(27, 37, 106, 116)    
  </code></pre>
  
  <p>The original rectangle should not be modified.</p>
  
  <pre><code> &gt;&gt;&gt; r
    Rectangle(30, 40, 100, 110)
  </code></pre>
  
  <p>Negative values should return a shrunken rectangle.</p>
  
  <pre><code>&gt;&gt;&gt; r.expand(-5)
    Rectangle(35, 45, 90, 100)
  </code></pre></li>
  </ol>
  
  <p>I can not figure out number 9 but I believe I am doing other things correct here is what I have so far:</p>
  
  <pre><code>class Rectangle:
  
      def __init__(self, X, Y, W, H):
  
         self.x = X
  
         self.y = Y
  
         self.w = W
  
         self.h = H
  
      def complete(self):
          return self.x,self.y,self.w,self.h
  
      def right(self):
          return self.x + self.w
  
      def __str__(self):
          return('Rectangle(' + str(self.x) + ',' + str(self.y) + ','
                      + str(self.w) + ',' + str(self.h)+')')
      def bottom(self):
          return self.y + self.h
  
      def __str__(self):
          return('Rectangle(' + str(self.y) + ',' + str(self.h) + ','
                 + str(self.y) + ',' + str(self.h) + ')')
      def size(self):
          return self.w,self.h
  
      def position(self):
          return self.x,self.y
  
      def area(self):
          return self.w * self.h
  
      def expand(self):
          pass
  
  print(Rectangle(5,10,50,100).complete())
  
  print(Rectangle(3, 5, 10, 20).right())
  
  print(Rectangle(12, 10, 72, 35).right())
  
  print(Rectangle(5,7,10,6).bottom())
  
  print(Rectangle(1,2,3,4).size())
  
  print(Rectangle(1,2,3,4).position())
  
  print(Rectangle(1,2,3,4).area())
  </code></pre>
  
  <p>Any help is much appreciated.</p>