Python

From Genetics Wiki
Revision as of 19:27, 14 August 2018 by Floyd (talk | contribs) (Created page with "An example from Glenn of working with objects in a list. <pre> class Dog: # Make class called 'Dog'. def __init__(self, name_of_dog): # Constructor, pass in the name o...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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')