Style Guide

The documentation will show examples a certain way, but it doesn't mean you have to write your code that way.

I have designed the documentation for what I believe is best for beginner students to work with the module and learn certain concepts.

Below I outline some examples of this:

Importing Easy Draw

In all examples Easy Draw is imported the following way:

import easy_draw

easy_draw.load_canvas()

square = easy_draw.Rectangle(
    xy = (100, 100), 
    width = 200, 
    height = 200
)

easy_draw.end()

This mean in order to use any of the Easy Draw commands, you need to use the module name and a dot operator.

Example: easy_draw.load_canvas()

My philosophy: It is best practice to have students work with modules using full module name first, but it does not prevent you for doing the following examples.

It is possible to indiscriminately import everything from a module at one fell swoop. This is called a wildcard import and it is done like this from module import * . It makes it so you do not need to reference the module to use its commands. See below:

from easy_draw import *

load_canvas()

square = Rectangle(
    xy = (100, 100), 
    width = 200, 
    height = 200
)

end()

Yes, the code is much less complex looking now. However, this is not Python best practice and wildcard imports should be avoided according to PEP 8. I want students to know they are working with a module and not abstract that experience too much.

You can also nickname the module when importing so you do not have to type easy_draw each time. You do this by using the import module as nickname structure.

import easy_draw as ed

ed.load_canvas()

square = ed.Rectangle(
    xy = (100, 100), 
    width = 200, 
    height = 200
)

ed.end()

I have done this with modules before and students seem to get confused about what the module's actual name is. Then when they come across code online that doesn't nickname the way they expect, they are confused. So, I only show this trick later on when they are more experienced or when a student asks "do I really have to type easy_draw each time?"

Using Object Constructors

In the examples you will notice that the object constructor functions are stylized to have each argument (property value) on separate lines:

square = easy_draw.Rectangle(
    xy = (100, 100), 
    width = 200, 
    height = 200
)

I have students stylize the constructor method calls this way so it is easier for them to see the individual property values of the object they are instantiating.

Of course you can put these values all on one line which is the typical way to call functions and pass argument values.

square = easy_draw.Rectangle(xy = (100, 100), width = 200, height = 200)

You may also notice that each example explicitly states the instance variable (property) name when assigning it a value in the constructor function call. I use this practice to help students learn the parameter needed to construct the object. Previous to doing this students would plug in values and not really know what the value was for. Explicitly using the name ensures the student knows what each value is used for. Example:

square = easy_draw.Rectangle(
    xy = (100, 100), 
    width = 200, 
    height = 200
)

But, of course, you can use positional arguments and provide the values in the order the constructor function expects them. Rectangle(xy, width, height)

square = easy_draw.Rectangle((100, 100), 200, 200)

However, the optional properties of an object (like color) are not positional arguments. These properties are keyword arguments and need to use the instance variable (property) name to set the value.

square = easy_draw.Rectangle((100, 100), 200, 200, color = "red")

Function and Property Names

All constructor functions start with a capital letter and use CamelCasing.

Examples: Rectangle(), Oval(), Text(), RegPolygon()

All other functions and instance variables/property names use all lowercase letters and snake_casing.

Examples: load_canvas(), color, .rotate(), border_width

Storing Objects in Variables

All examples show objects being stored in a variable. If you want to be able to use a method on an object or change a property of that object, you must store it in a variable to reference the object later. So, all examples show objects being stored and referenced from a variable.

square = easy_draw.Rectangle(
    xy = (100, 100), 
    width = 200, 
    height = 200
)

square.rotate(45)

The above example shows the Rectangle object is stored in a variable named square. Because it stored in a variable, we can use the .rotate() method on it.

However, the shape will be drawn without storing the object in a variable.

easy_draw.Rectangle(
    xy = (100, 100), 
    width = 200, 
    height = 200
)

If you try the above, it still creates the square.

You can call methods on an object without storing it in a variable by immediately following the constructor function call with the method call.

easy_draw.Rectangle(
    xy = (100, 100), 
    width = 200, 
    height = 200
).rotate(45)

OR

easy_draw.Rectangle(xy = (100, 100), width = 200, height = 200).rotate(45)

Coordinates as Tuples or

Lots of shape drawing and game Python libraries will require x and y coordinates as separate values. I have found that students see these values as a pair, so Easy draw objects expect x and y coordinate to be coupled as a tuple.

Example: This -- xy =(100, 200) | Not this -- x = 100, y = 200

However, the points_list for Polygon and Line must be pairs of x and y (an even amount of numbers) but not in tuples, simply just listed:

Example:

  • This -- points_list = [50, 210, 180, 250, 300, 210]

  • Not this -- points_list = [(50, 210), (180, 250), (300, 210)]

Last updated