Ans1. Object-oriented programming (OOP) in Python is a way of writing code that makes it easier to understand, maintain, and reuse.
OOP does this by grouping related data and functions together into objects. This makes the code more organized and modular, which makes it easier to understand and maintain. It also makes the code more reusable, because programmers can create new objects that inherit from existing objects.
Ans2. An inheritance search looks for an attribute in the following places:
i) The instance object itself.
ii) The class that the instance was created from.
iii) All higher superclasses, progressing from left to right (by default).
The search stops at the first place the attribute is found.
Ans3. a class object is a reference to the class itself, while an instance object is a reference to an object that was created from a class. The type() function, the __class__ attribute, and the isinstance() function can all be used to distinguish between class objects and instance objects.
Ans4. The first argument in a class's method function is special because it refers to the instance of the class that the method is being called on. This argument is typically named self.
Ans5. The init method is the constructor for a class. It is called when an instance of the class is created. The init method is used to initialize the attributes of the instance.
Ans6. To create a class instance, you use the __init__() method. The __init__()method takes in the arguments that you want to pass to the instance and initializes the attributes of the instance.
Ans7. To create a class, you use the class keyword. The class keyword takes in the name of the class and the body of the class. The body of the class can contain methods, attributes, and other class definitions.
Ans8. The superclasses of a class are the classes that the class inherits from. The superclasses of a class are defined in the __bases__ attribute of the class.
Ans9. Classes and modules are both ways of organizing code in Python. Classes are used to create objects, while modules are used to organize code that is not related to objects. Classes are defined in modules, but they can also be imported from other modules.
Ans10. Instances of a class are created using the __init__() method. The __init__() method takes in the arguments that you want to pass to the instance and initializes the attributes of the instance.
Classes are created using the class keyword. The class keyword takes in the name of the class and the body of the class. The body of the class can contain methods, attributes, and other class definitions.
Ans11. Class attributes are created in the body of the class. They can be accessed using the __class__ attribute of an instance.
Ans12. Instance attributes are created in the __init__() method of the class. They can be accessed using the __self__ attribute of an instance.
Ans13. The term self in a Python class refers to the instance of the class that the method is being called on.
Ans14. Operator overloading in Python allows you to define how operators work with your own classes. For example, you could define how the + operator works with your own class to add two instances of the class together.
Ans15. if you have a class that represents numbers, it might make sense to allow operator overloading of the + operator so that you can add two instances of the class together.
i) When you want to create a class that represents a mathematical object, such as a vector or a matrix.
ii)When you want to create a class that represents a data structure, such as a list or a dictionary.
iii) When you want to create a class that represents a user interface element, such as a button or a checkbox.
Ans16. The most popular form of operator overloading in Python is the __add__() method. The __add__() method is called when the + operator is used with two instances of the class.
Ans17. The two most important concepts to grasp in order to comprehend Python OOP code are classes and objects. Classes are the blueprints for objects, and objects are the instances of classes.
Ans18. Three applications for exception processing are:
- To handle errors that occur during program execution.
- To provide a way to gracefully exit a program.
- To provide a way to log errors for debugging
Ans19. If you don't do something extra to treat an exception, your program will crash. This means that the program will stop executing and you will not be able to get any further output.