objects in python
# Online Python compiler (interpreter) to run Python online.
class Dog:
# Class attribute
species = "Canis familiaris"
# Constructor method
def __init__(self, name1, age):
self.name= name1#jo pass karoge vo variable me jayega and variable ka name kuch bhi ho sakta hai bcz we access that with the help of variable name and due to self means it denotes objects same variable name is used for multiple objects bcz we refer variable by object likke self.name here self is object name it is referring to that object # Instance attribute
self.age= age # Instance attribute
# Instance method
def bark(self):
print(f"{self.name} barks!")
# Instance method
def get_info(self):
return f"{self.name} is a {self.species} and is {self.age} years old."
d1 = Dog("hi",20)
info=d1.get_info()
print(info)
Comments
Post a Comment