Difference between revisions of "Python"

From Genetics Wiki
Jump to: navigation, search
(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...")
 
(No difference)

Latest revision as of 19:27, 14 August 2018

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')