Python Sorting Part 2: Classes and Attributes
Python Sorting with Classes and Attributes # Here we define a class, containing student names # each student contains an list attribute of varying from 1 to 7 in length # populated by random integer values from 0 to 500 import random # imports the random module # declares the class object class myClass(object): def __init__(self, inputName): self.myName = inputName # creates a myName attribute # create a list of random length self.myList = [random.randint(0,500) \ for x in range(random.randint(1, 7))] def greet(self): # prints out name and list of values the instance holds print 'Hi I am {}, my list is {}'.format(self.myName, self.myList) random.seed(0) # providing a seed to generate consistent random values # Now we create instances of myClass for 5 students, # and store the result as a list in studentNames studentNames = ('tommy', 'sally', 'summer'