classes: attributes and methods


requirements

test_classes


test_classes_w_attributes

I now add some tests for attributes since I know how to define a class for attributes

red: make it fail

  • I add a failing test to TestClasses in classes.py

    def test_classes_w_attributes(self):
        self.assertEqual(classes.ClassWithAttributes.a_boolean, bool)
    

    the terminal shows AttributeError

  • I add a class definition to classes.py

    class ClassWithAttributes(object):
    
        pass
    

    the terminal shows AttributeError for a missing attribute in the newly defined class

green: make it pass

refactor: make it better

red: make it fail

Let us add more tests with the other Python data structures to test_classes_w_attributes

def test_classes_w_attributes(self):
    self.assertEqual(classes.ClassWithAttributes.a_boolean, bool)
    self.assertEqual(classes.ClassWithAttributes.an_integer, int)
    self.assertEqual(classes.ClassWithAttributes.a_float, float)
    self.assertEqual(classes.ClassWithAttributes.a_string, str)
    self.assertEqual(classes.ClassWithAttributes.a_tuple, tuple)
    self.assertEqual(classes.ClassWithAttributes.a_list, list)
    self.assertEqual(classes.ClassWithAttributes.a_set, set)
    self.assertEqual(classes.ClassWithAttributes.a_dictionary, dict)

the terminal shows AttributeError

green: make it pass

I add matching attributes to ClassWithAttributes to make the tests pass

class ClassWithAttributes(object):

    a_boolean = bool
    an_integer = int
    a_float = float
    a_string = str
    a_tuple = tuple
    a_list = list
    a_set = set
    a_dictionary = dict

and the terminal shows all tests pass


test_classes_w_methods

I can also define classes with methods which are function definitions that belong to the class

red: make it fail

I add some tests for class methods to TestClasses in classes.py

def test_classes_w_methods(self):
    self.assertEqual(
        classes.ClassWithMethods.method_a(),
        'You called MethodA'
    )

and the terminal shows AttributeError

green: make it pass

  • I add a class definition to classes.py

    class ClassWithMethods(object):
    
        pass
    

    the terminal now gives AttributeError with a different error

  • When I add the missing attribute to the ClassWithMethods class

    class ClassWithMethods(object):
    
        method_a
    

    the terminal shows NameError because there is no definition for method_a

  • I define method_a as an attribute by pointing it to None

    class ClassWithMethods(object):
    
        method_a = None
    

    the terminal shows TypeError since method_a is None which is not callable

  • I change the definition of method_a to make it a function which makes it callable

    class ClassWithMethods(object):
    
        def method_a():
            return None
    

    and the terminal shows AssertionError. Progress!

  • I then change the value that method_a returns to match the expectation of the test

    def method_a():
        return 'You called MethodA'
    

    and the test passes

refactor: make it better

  • I can “make this better” by adding a few more tests to test_classes_w_methods for fun

    def test_classes_w_methods(self):
        self.assertEqual(
            classes.ClassWithMethods.method_a(),
            'You called MethodA'
        )
        self.assertEqual(
            classes.ClassWithMethods.method_b(),
            'You called MethodB'
        )
        self.assertEqual(
            classes.ClassWithMethods.method_c(),
            'You called MethodC'
        )
        self.assertEqual(
            classes.ClassWithMethods.method_d(),
            'You called MethodD'
        )
    

    the terminal shows AttributeError

  • and I change each assertion to the right value until they all pass


test_classes_w_attributes_and_methods

Since I know how to define classes with methods and how to define classes with attributes, what happens when I define a class with both?

red: make it fail

I add another test for a class that has both attributes and methods

def test_classes_w_attributes_and_methods(self):
    self.assertEqual(
        classes.ClassWithAttributesAndMethods.attribute,
        'attribute'
    )
    self.assertEqual(
        classes.ClassWithAttributesAndMethods.method(),
        'you called a method'
    )

the terminal shows AttributeError

green: make it pass

I make classes.py to make the tests pass by defining the class, attribute and methods

class ClassWithAttributesAndMethods(object):

    attribute = 'attribute'

    def method():
        return 'you called a method'

test_object_attributes_and_methods

To view what attributes and methods are defined for any object I can call dir on the object.

The dir method returns a list of all attributes and methods of the object provided to it as input

red: make it fail

I add a test to test_classes.py

def test_object_attributes_and_methods(self):
  self.assertEqual(
      dir(classes.ClassWithAttributesAndMethods),
      []
  )

the terminal shows AssertionError as the expected and real values do not match

green: make it pass

I copy the values from the terminal to change the expectation of the test

def test_object_attributes_and_methods(self):
    self.assertEqual(
        dir(classes.ClassWithAttributesAndMethods),
        [
            '__class__',
            '__delattr__',
            '__dict__',
            '__dir__',
            '__doc__',
            '__eq__',
            '__format__',
            '__ge__',
            '__getattribute__',
            '__gt__',
            '__hash__',
            '__init__',
            '__init_subclass__',
            '__le__',
            '__lt__',
            '__module__',
            '__ne__',
            '__new__',
            '__reduce__',
            '__reduce_ex__',
            '__repr__',
            '__setattr__',
            '__sizeof__',
            '__str__',
            '__subclasshook__',
            '__weakref__',
            'attribute',
            'method',
        ]
    )

and it passes, the last 2 values in the list are attribute and method which I defined earlier


review

CONGRATULATIONS! If you made it this far and typed along with me, You know

  • how to define a class with an attribute

  • how to define a class with a method

  • how to define a class with an initializer

  • how to view the attributes and methods of a class

Would you like to test_classes_w_initializers?


classes: tests and solutions