class MyClass(object): def give_me_a_name(self, name): self.name = name # create an instance called my_object # set the name of my_object to 'Tris' using give_me_a_name # print my_object.name # set the name through direct access, and print result class Person(object): """creates a person with the name Ada""" def __init__(self): self.name = "Ada" # create an instance of Person called person # print person.name class Person(object): def __init__(self, name, title="Programmer"): """Create Person object with name and title""" self.name = name self.title = title ## create a new object called grace, setting the name to 'Grace Hopper' ## create a new object called 'Ada Lovelace' and change her title to 'Mother of Computing' class MakeList(object): something = [] def append_input(self, input): self.something.append(input) firstlist = MakeList() secondlist = MakeList() ## put 23 into the 'something' list of your first instance #what will be in the something attribute for firstlist #what about secondlist? # Lets make a quick test so you can see if your solution works def test_MakeList(): first = MakeList() second = MakeList() first.append_input(42) print first.something, second.something assert (first.something == second.something) is False #this should raise an Assertion Error test_MakeList() ## make a new class, create something in the __init__ method ## Test it! test_MakeList() # make a generic creature class class Creature(object): def __init__(self, name, weight, gender): """Create instance of creature with a name, weight, and gender""" self.name = name self.weight = weight self.gender = gender def __lt__(self, other): """check if weight is less than other""" return self.weight < other def __gt__(self, other): """check if weight is greater than other""" return self.weight > other def __et__(self, other): """check of weight is equal to other""" return self.weight == other def __ne__(self, other): """check if weight is not equal to other""" return not self.weight == other def __str__(self): """name, weight and gender of specific creature instance""" return 'Name: {} is a {} with weight {:.2f}'.format(self.name, self.gender, self.weight) ## Create a few animals animal_a = Creature('001', 34.2,'Female') animal_b = Creature('002', 36.7, 'Male') animal_c = Creature('003', 32.4, 'Male') ## Check comparison is on weight ## Now if we put these creatures in a list, we can sort them by weight ## sort list by weight class A(object): a = 'a is a' class B(A): b = 'b is b' class C(B): b = 'b is 42' c = 'c is c' c_instance = C() # Is a an attribute of c_instance? #Is b an attribute of c_instance? # create a new instance of A, what is the value of the attribute c? ## example where our class fails class Creature(object): def __init__(self, name, weight, gender): """Create instance of creature with a name, weight, and gender""" self.name = name self._weight = 0 self.weight = weight self.gender = gender @property def weight(self): # getter return self._weight @weight.setter def weight(self, val): # setter check val is float if isinstance(val, float): self._weight = val else: raise ValueError('Weight must be a float, not {}:{}'.format(type(val),val)) def __lt__(self, other): """check if weight is less than other""" return self.weight < other def __gt__(self, other): """check if weight is greater than other""" return self.weight > other def __et__(self, other): """check of weight is equal to other""" return self.weight == other def __ne__(self, other): """check if weight is not equal to other""" return not self.weight == other def __str__(self): """name, weight and gender of specific creature instance""" return 'Name: {} is a {} with weight {:.2f}'.format(self.name, self.gender, self.weight) ## Now what happens when we pass a bad weight value? ## create a good_creature named 'bird_001', with weight 32.5, and gender 'Female' ## print result ## create Creature Class using property to check gender input ## Check gender good_gender = Creature('Bruce', 32.4, 'Male') print good_gender bad_gender = Creature('abby', 32.4, 'blue')