In this module you will learn to create a class based off of another class through inheritance.
Imagine that you have a class, Sprite, that represents a generic object in game. It may have properties like it's x and y position on screen, and it may have methods to move it to another position or two draw it on to the screen. Now, what if you want a specific object in the game, like a player. A player may have properties like name and score, but it would also share many properties and methods with an instance of Sprite. It wouldn't make sense to reimplement all of the methods that Sprite has in Player since they've already been created. Wouldn't it be easier to just use the ones already defined in Sprite?
This can be done with a language feature called inheritance. Inheritance allows one class to be based off of another class.
The term extends describes the situation where one class inherits from another class. The child class extends the parent class. When extending a class...
__init__ methodThe syntax for inheritance is as follows:
class SomeClassNameclass Player(Sprite):Below is an example of one class extending another. As you can see, methods in the base class, Ghost, can be called from instances of the sub class, FriendlyGhost even though they're not directly defined in FriendlyGhotst.
Sample Program: FriendlyGhost does not have a make_noise method explicitly defined, yet it can be called on an instance of FriendlyGhost because it FriendlyGhost inherits make_noise from the parent class, Ghost.
It sometimes useful to change the behavior of an inherited method.
Sample Program: FriendlyGhost overrides the make_noise method so that it prints out hi there! instead of boo!
Finally, it may be helpful to call methods from the super class.
super() method__init__ method, use: super().__init__()⚠️ this method of calling super class methods works with "single" inheritance
You can see a full example of inheritance, overriding methods, and calling super in the video below.
Python offers a module that helps create tests for checking your code. The process of testing individual functions in your code is called unit testing.
The module for helping with creating unit tests is called (not surprisingly!) unittest. The unittest framework can be used to:
unittesetTo test a function that you've created in a separate module:
unittest module: import unittestimport mymoduleunittest.TestCase: class TestMyModule(unittest.TestCase)test: def test_some_case(self)self.assertEquals(observed, expected)self.assertEquals(mymodule.function(some_arg), some_expected_value)unittest.main()unittest OutputThe output of the test starts with as series of characters composed of F and ., where every F signifies a test failure, and . signififes a passing test:
...FF.
The output above shows that two out of 6 tests fail.
After that, details of each failing test will be printed out.
Here's what an example function to be tested and accompanying test code may look like:
In mymodule.py:
def increment(n):
return n + 1
In test_mymodule.py:
import unittest
from mymodule import increment
class TestIncrement(unittest.TestCase):
def test_adds_one(self):
self.assertEquals(increment(2), 3)
if __name__ == '__main__':
unittest.main()
Finally, here's an example of test driven development where tests are written first prior to the actual implementation of the module / function.
Now that you've completed this module, please visit our NYU Classes site and take the corresponding quiz for this module. These quizzes are worth 5% of your total grade and are a great way to test your Python skills! You may also use the following scratch space to test out any code you want.