def test_animal_creation(self): murph = Dog("Murph") self.assertIsInstance(murph, Dog) self.assertIsInstance(murph, Animal) self.assertIsInstance(self.jacob, Animal)
def test_dog_walks_at_speed(self): # creating a separate test class here because there is no "stop walking" method. self.bill = Dog('Bill') self.bill.set_legs(4) self.assertEqual(self.bill.speed, 0) self.bill.walk() self.assertEqual(self.bill.speed, (self.bill.speed + (0.2 * self.bill.legs)))
def test_dog_walk(self): self.doggo = Dog("Doggo") self.doggo.walk() self.assertEqual(0, self.bob.speed) self.doggo.set_legs(4) self.doggo.walk() self.assertEqual(self.doggo.speed, .8)
def test_animal_creation(self): bob = Dog("Bob") self.assertIsInstance(bob, Dog) self.assertIsInstance(bob, Animal)
def setUpClass(self): self.bob = Dog("Bob")
from animals import Dog d3 = Dog("Old Yeller", 10) print d3.run()
from animals import Dog myDog = Dog("Pichula", "Orejero Aleman", 5, 30) myDog.bark() print(myDog.getName()) print(myDog.getRace()) print(myDog.getAge()) print(myDog.getWeigth()) myDog.eat() myDog.age() myDog.bark() print(myDog.getName()) print(myDog.getRace()) print(myDog.getAge()) print(myDog.getWeigth()) myDog.poop(2) print(myDog.getWeigth())
def setUpClass(self): self.bob = Dog("Bob") self.jacob = Animal("Jacob")
DrobnyMuzikSTaskou.inventory['taška'].append('celý lup') DrobnyMuzikSTaskou.disappear() Venku.spawn(DrobnyMuzikSTaskou) del (Zalozna.DrobnyMuzikSTaskou) time.sleep(0) Zalozna.spawn(Trachta(experience=float('infinity'))) repr(Trachta) Trachta.say( SlecnaTicha, 'Žádné strachy, slečno Tichá.'.join( ['', 'Však my si ho najdeme', 'My mu tipec zatneme.'])) SlecnaTicha.worry(False) for b in [0, random.choice((2, 3))]: Trachta.whistle.blow() Doubrava = Dog(sex='fenka', name='Doubrava') Zalozna.spawn(Doubrava) Doubrava.sniff().sniff() repr(Doubrava) for i in [Trachta, Doubrava, SlecnaTicha]: Venku.spawn(i) del (Zalozna.i) Venku.spawn([Blato(), Strom()]) Trachta.say(Doubrava, 'Čile za ním!') Doubrava.ignore().procrastinate() chlapci = [] for m in range(0, 6): chlapec = Man(name='Chlapec #%d' % m)
def setUpClass(self): self.bob = Dog("Bob") self.rob = Animal("Rob")
def fn(): pass print(type(fn) == types.FunctionType) print(type(abs) == types.BuiltinFunctionType) print(type(lambda x: x) == types.LambdaType) print(type((x for x in range(10))) == types.GeneratorType) # 使用isinstance() class Husky(Dog): def run(): print("Husky is running...") d = Dog() h = Husky() print(isinstance(h, Husky)) print(isinstance(h, Dog)) print(isinstance(h, Animal)) print(isinstance('a', str)) print(isinstance(123, int)) print(isinstance([1, 2, 3], (list, tuple))) print(isinstance((1, 2, 3), (list, tuple))) print(dir('ABC')) print(len('ABC')) print('ABC'.__len__())
def setUpClass(self): self.bob = Dog('Bob')
def setUpClass(): critter = Animal() scooby = Dog(Animal) print(scooby)
from mathdojo import MathDojo from hospital import Patient from hospital import Hospital from car import Car from callcenter import Call from callcenter import CallCenter from bike import Bike from animals import Animal from animals import Dog from animals import Dragon md = MathDojo(0) print md hosp = Hospital("Great Hospital", 6) print hosp patient = Patient(1, "Bob") print patient car = Car(5000, "75mph", "Full", "25mpg") print car call = Call(1, "Stephen", "770-789-7038", 8.30, "For Fun") print call call_center = CallCenter() print call_center bike = Bike(150, "15mph") print bike fox = Animal("Fox") print fox pitbull = Dog("Pitbull") print pitbull trogdor = Dragon("Trogdor the Burninator") print trogdor
from animals import Dog from animals import Cat from animals import Bird dude = Dog('Dude', '1/1/2011', 'Brown', 'Jack Russell') oogs = Cat('Oogie', '1/1/2006', 'Grey', 'Fluffy') bbird = Bird('Big Bird', '10/11/1969', 'Yellow', 'Canary') dude.printit() oogs.printit() bbird.printit()
""" Method overloading. Create a base class named Animal with a method called talk and then create two subclasses: Dog and Cat, and make their own implementation of the method talk be different. For instance, Dog’s can be to print ‘woof woof’, while Cat’s can be to print ‘meow’. Also, create a simple generic function, which takes as input instance of a Cat or Dog classes and performs talk method on input parameter. """ from animals import Dog, Cat, animal_talk if __name__ == "__main__": dog = Dog() cat = Cat() animal_talk(dog) animal_talk(cat)
def add_Dog(self): newdog = Dog() newdog.put() self.Mypets.append(newdog)
animal_classes = [Parrot, Penguin, Dog, HouseCat, BobCat] colors = ["White", "Black", "Red", "Green", "Blue", "Striped"] habitats = ["Land", "Sea", "Air", "Tree", "Campus"] random_true_false = [True, False] zoo = [] zoo_size = 10 for _ in range(zoo_size): spec = animal_classes[random.randint(0, 4)] age = random.randint(1, 10) color = random.choice(colors) habitat = random.choice(habitats) if spec == Parrot: new_animal = Parrot(age, color, random.choice(random_true_false)) elif spec == Penguin: new_animal = Penguin(age, color) elif spec == Dog: new_animal = Dog(age, color) elif spec == HouseCat: new_animal = HouseCat(age, color) else: # BobCat try: new_animal = BobCat(age, color, habitat) except ValueError as value_error: print(habitat + " is " + str(value_error)) continue zoo.append(new_animal) print("There are {} animals in the zoo".format(len(zoo))) for animal in zoo: print('{} says {}'.format(str(animal), animal.sound()))