+ 1
Someone explain to me python class in layman's language . This topic confuse me alot.
2 Answers
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.
0
thax