Skip to content

Latest commit

 

History

History

README.md

Python - Classes and Objects

In this learning experience, I learnt the following:

  • What is a class
  • What is an object and an instance
  • What is the difference between a class and an object or instance
  • What is an attribute
  • What is a method
  • What is the special init method and how to use it etc.

Tasks 📃

Task-0: The 0-square.py file contains an empty class Square that defines a square

Task-1: The 1-square.py file contains a class Square that defines a square by: (based on 0-square.py)

  • Private instance attribute: size
  • Instantiation with size (no type/value verification)

Task-2: The 2-square.py file contains a class Square that defines a square by: (based on 1-square.py)

  • Private instance attribute: size
    • Instantiation with optional size: def __init__(self, size=0):
    • size must be an integer, otherwise raise a TypeError exception with the message size must be an integer
  • if size is less than 0, raise a ValueError exception with the message size must be >= 0

Task-3: The 3-square.py contains a class Square that defines a square by: (based on 2-square.py)

  • Private instance attribute: size
  • Instantiation with optional size: def __init__(self, size=0):
    • size must be an integer, otherwise raise a TypeError exception with the message size must be an integer
    • if size is less than 0, raise a ValueError exception with the message size must be >= 0
  • Public instance method: def area(self): that returns the current square area

Task-4: The 4-square.py contain a class Square that defines a square by: (based on 3-square.py)

  • Private instance attribute: size:
    • property def size(self): to retrieve it
    • property setter def size(self, value): to set it:
      • size must be an integer, otherwise raise a TypeError exception with the message size must be an integer
      • if size is less than 0, raise a ValueError exception with the message size must be >= 0
  • Instantiation with optional size: def __init__(self, size=0):
  • Public instance method: def area(self): that returns the current square area

Task-5: The 5-square.py file contains a class Square that defines a square by: (based on 4-square.py)

  • Private instance attribute: size:
    • property def size(self): to retrieve it
    • property setter def size(self, value): to set it:
      • size must be an integer, otherwise raise a TypeError exception with the message size must be an integer
      • if size is less than 0, raise a ValueError exception with the message size must be >= 0
  • Instantiation with optional size: def __init__(self, size=0):
  • Public instance method: def area(self): that returns the current square area
  • Public instance method: def my_print(self): that prints in stdout the square with the character #:
    • if size is equal to 0, print an empty line

Task-6: The 6-square.py file contains a class Square that defines a square by: (based on 5-square.py)

  • Private instance attribute: size:
    • property def size(self): to retrieve it
    • property setter def size(self, value): to set it:
      • size must be an integer, otherwise raise a TypeError exception with the message size must be an integer
      • if size is less than 0, raise a ValueError exception with the message size must be >= 0
  • Private instance attribute: position:
    • property def position(self): to retrieve it
    • property setter def position(self, value): to set it:
      • position must be a tuple of 2 positive integers, otherwise raise a TypeError exception with the message position must be a tuple of 2 positive integers
  • Instantiation with optional size and optional position: def __init__(self, size=0, position=(0, 0)):
  • Public instance method: def area(self): that returns the current square area
  • Public instance method: def my_print(self): that prints in stdout the square with the character #:
    • if size is equal to 0, print an empty line
    • position should be use by using space - Don’t fill lines by spaces when position[1] > 0