1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

Classes

If you read about variables, repetition and commands you already know a great deal about how to keep your code tidy and organized. Variables can be used to store data, commands to manipulate data, and for-loops to do things (e.g. traverse lists) multiple times. As you move on to bigger projects you'll probably want to learn about classes as well. A class is a programming convention that defines a thing (or object) together with all of the thing's properties (e.g. variables) and behavior methods (e.g. commands). If you take a look at the source code of NodeBox libraries you'll notice that they are full of classes.

For example, a Creature class would consist of all the traits shared by different creatures, such as size, position, speed and direction (properties), and avoid_obstacle(), eat_food() (methods).

We can then use the Creature class to define many different instances of a creature. For example, an ant is a small and speedy creature that wanders around randomly looking for food, avoiding obstacles by going around them. A dinosaur is also a creature, but bigger and slower. It eats anything crossing its path and avoids obstacles by crashing through them.

  • class: the abstract characteristics (properties and methods) of a thing.
  • object: one particular instance of a class.
  • properties: variables tied to an instance of a class.
  • methods: commands that manipulate an instance's properties.

 


Class definition and initialization

Each class has an __init__() method which is executed once when an instance of the class is created. The __init__() method sets all the starting values for an object's properties.

For example, here's what a simple definition of a Creature class would look like:

class Creature:
    
    def __init__(self, x, y, speed=1.0, size=4):
        
        self.x = x
        self.y = y
        self.speed = speed
        self.size = size

As you can see a creature has x and y properties (the location of the creature) and speed and size properties (which we'll use to move the creature around). All of them can be set when a new creature is "instantiated". The speed and size properties have default values so they are optional.

ant = Creature(100, 100, speed=2.0)
dinosaur = Creature(200, 250, speed=0.25, size=45)

To change a property value for a creature later on we can simply do:

ant.speed = 2.5

Naming conventions:

  • Class names use CamelCase notation.
  • Properties that are only for private use inside the class start with _

 


Methods and the self parameter

Let's add a roam() method to the Creature class to move creatures around randomly. A class method looks exactly like a command definition but it always takes self as the first parameter. The self parameter is the current instance of the class. It allows you to access all of an object's current property values and methods from inside the method.

class Creature: 
    
    def __init__(self, x, y, speed=1.0, size=4):
        
        self.x = x
        self.y = y
        self