+ 1

Someone explain to me python class in layman's language . This topic confuse me alot.

28th Apr 2018, 7:52 AM
Isaac Kakodwa
Isaac Kakodwa - avatar
2 Respostas
0
Python has classes and objects. Classes are the ‘blueprints’ for all objects, they define what variables and functions (methods) the object can use and store. For example... class Dog: def makeSound(self): print("Woof") myDog = Dog() # myDog is an object print(myDog.makeSound()) # . operator is used to access the object’s variables and functions As you can see I made a Dog class with one method called makeSound(), every object function must have the first parameter referring to the object its standard to use the word self. After the class was made I created an object and called the object’s makeSound() method. Now for a harder example... class Animal: def __init__(self, name, sound): self.name = name # self is the object using this method self.sound = sound myDog = Animal("Dog", "Woof") # constructor is run print(myDog.sound) __init__ is something called a constructor which runs when the object is created thats why when the myDog object was being created the Animal() class took in arguments.
28th Apr 2018, 9:18 AM
TurtleShell
TurtleShell - avatar
0
thax
28th Apr 2018, 9:58 AM
Isaac Kakodwa
Isaac Kakodwa - avatar