Skip main navigation

Share your code with other people

Share your code with other people
OK. So I’m just going to show you how I’ve added some doc strings to my class. So this is the item class. As you can see, I’ve added these things in triple quotes, which come up green on the screen. And I’m just going to add another one to show you an example. It’s just simply typing in a description of what each of the methods for that particular class does. I’ve done that for all of the classes that are here. But these are needed so that we have something that comes up in our documentation. Then I’m going to open the command prompt, and I’m going to change directory to be in the same directory where all of my files are stored.
So mine was in one called desktop oop week four and then rpg. I could have done that all in one, but I just did it separately to show you. And then I’m going to type in this particular command, which is the command to create the document. And it does it automatically. So I literally just typed that in. And then as you can see, in my folder I’ve now got some HTML files.
And if we go and investigate inside one of these– so this is the one for character– we can see that we are given some documentation automatically generated so that all of the different methods are there, and our doc strings, which we wrote earlier, are available for us to have a look at.

Sharing your code with other people need not be limited to people you know. It is very easy to share your code online so that anyone, anywhere can use it.

This probably sounds scary – after all, this is your first object-oriented program. However, you might be surprised and pleased by other people’s reaction to your code – you might get offers of help, or you might make someone’s day by saving them a lot of time!

You can use a website such as GitHub to share your code so that other people can use it, and maybe even suggest changes. Follow this guide for beginners to get your code online. All the code you have downloaded in this course is on GitHub for anyone to see and use.

If you are sharing your code, it is also useful to provide some documentation so that people know what classes and methods are available, and how to use them. Python has a built-in feature for automatically creating basic documentation. Once again, you will only be able to use this feature if you are using Python installed on your computer rather than using Trinket.

You will need to add docstrings to your code, which will form the basis of the documentation. This is very easy – simply add a line of explanation at the start of each function and enclose it in triple quotation marks, like this:

def get_description(self):
"""Returns a string containing the description of the room"""
return self.description

Next, open a terminal window (search ‘cmd’ on Windows) and navigate to your rpg folder using the cd command, for example:

cd PathToMyCode
cd rpg

Type in the following command to generate documentation for all files in this folder:

On Windows

python -m pydoc -w . 

On Mac/Linux

python3 -m pydoc -w ./ 

If you get an error when you try this command, take a look at this resource, and make sure you have added Python to your path.

This command should generate HTML files in your directory with the same names as your class files. Open the files to see the documentation for your package.

This article is from the free online

Object-oriented Programming in Python: Create Your Own Adventure Game

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now