Python

From Genetics Wiki
Jump to: navigation, search

An example from Glenn of working with objects in a list.

class Dog:  # Make class called 'Dog'.
    def __init__(self, name_of_dog):  # Constructor, pass in the name of the dog.
        self.name = name_of_dog
        self.number_of_tails = 2

    def bark(self):  # Every dog can bark.
        print('Woof')

# Create one instance (object) of the Dog class named 'Bella', saved to the name d_1
d_1 = Dog('Bella')

# Create 10 instances of the Dog class named 'Lucy', saved to the list 'p'
p = []
for i in range(10):
    p[i] = Dog('Lucy')