Important Question Mad
Important Question Mad
Important Question Mad
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
To create an object from this class, we can use the following code:
We can then call the `say_hello` method on these objects to print out a
message:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_hello(self):
print(f"Hello, my name is {self.name} and I am {self.age}
years old.")
class MyClass:
def __init__(self, value):
self.value = value
def __repr__(self):
return f"MyClass({self.value})"
class MyClass:
def __init__(self, value):
self.value = value
def __str__(self):
return f"MyClass object with value {self.value}
def __del__(self):
print("Destroying object")
class MyClass:
def __init__(self, name, age):
self.name = name
self.age = age
def print_info(self):
print(f"Name: {self.name}, Age: {self.age}")
To create an instance of the class and call its methods, you can do the
following:
```python
class MyClass:
def __init__(self):
self.public_attr = "This is a public attribute"
self._protected_attr = "This is a protected attribute"
self.__private_attr = "This is a private attribute"
def public_method(self):
print("This is a public method")
def _protected_method(self):
print("This is a protected method")
def __private_method(self):
print("This is a private method")
obj = MyClass()
● When creating an instance of the class, you can access the public
attributes and methods directly using dot notation.
● The protected attributes and methods can also be accessed directly,
but it is recommended to treat them as private and not access them
from outside the class.
● Finally, the private attributes and methods cannot be accessed from
outside the class, and attempting to do so will raise an
`AttributeError`.
def display_info(self):
print(f"Name: {self.name}, Species: {self.species}")
class Dog(Animal):
def __init__(self, name, species, breed):
super().__init__(name, species)
self.breed = breed
print("Dog constructor called")
def display_info(self):
super().display_info()
print(f"Breed: {self.breed}")
class Person:
def __init__(self, name, age=18, occupation="Unemployed"):
self.name = name
self.age = age
self.occupation = occupation
def display_info(self):
print(f"Name: {self.name}, Age: {self.age}, Occupation:
ANSWER {self.occupation}")
def display_name(self):
print(f"Name: {self.name}")
# Creating two instances of the MyClass class
obj1 = MyClass("Object 1")
obj2 = MyClass("Object 2")
def make_sound(self):
print("Some generic sound")
def display_info(self):
print(f"Name: {self.name}, Species: {self.species}")
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name, "Canine")
self.breed = breed
def make_sound(self):
print("Bark bark!")
class Cat(Animal):
def __init__(self, name, color):
super().__init__(name, "Feline")
self.color = color
def make_sound(self):
print("Meow meow!")
def get_name(self):
return self.name
def get_department(self):
return self.department
def get_salary(self):
return self.salary
def print_info(self):
print(f"Name: {self.name}")
print(f"Department: {self.department}")
print(f"Salary: {self.salary}")
def get_balance(self):
return self.__balance