Quote:You will not be happy, here's what comes up in the highlighted top box:Yes i know
So to a take a example that i did rewrite in a other forum.
The tutorial is from here.
Here is the code from tutorial:
import random import sys import os class Animal: __name = "" __height = 0 __weight = 0 __sound = 0 def __init__(self, name,height,weight,sound): self.__name = name self.__height = height self.__weight = weight self.__sound = sound def set_name(self, name): self.__name = name def set_height(self, height): self.__height = height def set_weight(self, weight): self.__weight = weight def set_sound(self,sound): self.__sound = sound def get_name(self): return self.__name def get__height(self): return self.__height def get__weight(self): return self.__weight def get__sound(self): return self.__sound def get_type(self): print("Animal") def toString(self): return "{} is {} cm tall and {} kilograms and say {}".format(self.__name, self.__height, self.__weight, self.__sound) cat = Animal ('Whiskers', 33, 10 , 'Meow') print(cat.toString()) class Dog(Animal): __owner = "" def __init__(self, name, height, weight, sound, owner): self.__owner = owner super(Dog,self).__init__(name,height,weight,sound) def set_owner(self,owner): self.__owner = owner def get_owner(self): return self.__owner def get_type(self): print("Dog") def toString(self): return "{} is {} cm tall and {} kilograms and say {} His owner is {}".format(self.__name, self.__height, self.__weight, self.__sound, self.__owner) def multiple_sounds(self, how_many=None): if how_many is None: print(self.get__sound()) else: print(self.get__sound()* how_many) spot = Dog("Spot", 53, 27, "Ruff", "Derek") print(spot.toString())
I did a Rewrite of code over,no getters and setters.
To code is also shorten to 1/3:
class Animal: def __init__(self, name,height,weight,sound): self.name = name self.height = height self.weight = weight self.sound = sound def to_string(self): return "{} is {} cm tall and {} kilograms and say {}".\ format(self.name,self.height,self.weight,self.sound) cat = Animal ('Whiskers', 33, 10 , 'Meow') print(cat.to_string()) class Dog(Animal): def __init__(self, name, height, weight, sound, owner): self.owner = owner super(Dog,self).__init__(name,height,weight,sound) def to_string(self): return "{} is {} cm tall and {} kilograms and say {} His owner is {}".\ format(self.name,self.height,self.weight,self.sound,self.owner) def multiple_sounds(self, how_many=None): if how_many is None: return(self.sound) return(self.sound * how_many) spot = Dog("Spot", 53, 27, "Ruff", "Derek") print(spot.to_string()) print(spot.multiple_sounds(10))
Reply