Post by account_disabled on Dec 16, 2023 23:10:49 GMT -5
The object of the class is created, the next step is to use the object to access its attributes. To access the attributes of a class using the object, remember B2B Email List only the instance variables can be accessed using the object. The instance variables in our class are name and legs. class Animals: type=”mammals” def __init__(self,name,legs): self.name=name self.legs=legs def bark(self): if self.name==”dog”: print(“woof woof!!!”) else: print(“not a dog”) dog=Animals(“dog”,4) print(dog.name) print(dog.legs) As we can see, we are able to access instance variables using dot notation. Let’s explore the output. #Output dog 4 To access the functions inside the class or methods, we will be using the dot notation.
The example is illustrated below. class Animals: type=”mammals” def __init__(self,name,legs): self.name=name self.legs=legs def bark(self): if self.name==”dog”: print(“woof woof!!!”) else: print(“not a dog”) dog=Animals(“dog”,4) print(dog.name) print(dog.legs) print(dog.bark()) #Output dog 4 woof woof!!! In the above example, we can see that we are accessing the class method “bark” using the dog object we created. We can see that we are not using the “self” parameter in the function arguments. This is because we don’t require the use of “self” outside the class as the object itself is acting as self. Inheritance Inheritance is a process through which the class attributes and methods can be passed to a child class. The class from where the child class is inheriting is the parent class. The syntax for inheritance is illustrated below. #Inheritance class parent: class child(parent):
From the above illustration, we can see that for the inheritance syntax we are placing the parent class name as an argument to the child class. Let’s use the Animals class and make a child class called dog. This is illustrated below. class Animals: type=”mammals” def __init__(self,name,legs): self.name=name self.legs=legs def bark(self): if self.name==”dog”: print(“woof woof!!!”) else: print(“not a dog”) class Dog(Animals): def __init__(self,name,legs,breed): Animals.__init__(self,name,legs) self.breed=breed In the above example code, we made a dog class which is extending the animals class which we created before. We are also using the parameters from the Animals using the Animals.__init__(arguments) which has name and legs which will be inherited to the dog class.
The example is illustrated below. class Animals: type=”mammals” def __init__(self,name,legs): self.name=name self.legs=legs def bark(self): if self.name==”dog”: print(“woof woof!!!”) else: print(“not a dog”) dog=Animals(“dog”,4) print(dog.name) print(dog.legs) print(dog.bark()) #Output dog 4 woof woof!!! In the above example, we can see that we are accessing the class method “bark” using the dog object we created. We can see that we are not using the “self” parameter in the function arguments. This is because we don’t require the use of “self” outside the class as the object itself is acting as self. Inheritance Inheritance is a process through which the class attributes and methods can be passed to a child class. The class from where the child class is inheriting is the parent class. The syntax for inheritance is illustrated below. #Inheritance class parent: class child(parent):
From the above illustration, we can see that for the inheritance syntax we are placing the parent class name as an argument to the child class. Let’s use the Animals class and make a child class called dog. This is illustrated below. class Animals: type=”mammals” def __init__(self,name,legs): self.name=name self.legs=legs def bark(self): if self.name==”dog”: print(“woof woof!!!”) else: print(“not a dog”) class Dog(Animals): def __init__(self,name,legs,breed): Animals.__init__(self,name,legs) self.breed=breed In the above example code, we made a dog class which is extending the animals class which we created before. We are also using the parameters from the Animals using the Animals.__init__(arguments) which has name and legs which will be inherited to the dog class.