Skip to content
Advertisement

How can I copy a class in Python?

I’m using separate classes to create my characters in my game that I made using pygame. As you can see the classes are almost identical, with the only differences being the self.walkRight and self.walkLeft. So I would like to know a way that I can copy the first class and modify only the things that I want to change. How can I do that? The code:

JavaScript

Advertisement

Answer

Using class inheritance gives you access to all of the methods of the parent class as well as adding your own methods.

Here is one example of class inheritance. Notice how the parent class (FiveNumbers) can access its own methods. The child class (TenNumbers) can access its own methods along with its parent’s method.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement