Python Interview Questions and Answers
Related differences
Ques 81. What is polymorphism in Python?
By using polymorphism in Python we will understand how to perform a single task in different ways. For example, designing a shape is the task and various possible ways in shapes are a triangle, rectangle, circle, and so on.
Ques 82. Does Python make use of access specifiers?
Python does not make use of access specifiers and also it does not provide a way to access an instance variable. Python introduced a concept of prefixing the name of the method, function, or variable by using a double or single underscore to act like the behavior of private and protected access specifiers.
Ques 83. How can we create an empty class in Python?
Empty class in Python is defined as a class that does not contain any code defined within the block. It can be created using pass keywords and object to this class can be created outside the class itself.
Ques 84. Define Constructor in Python.
Constructor is a special type of method with a block of code to initialize the state of instance members of the class. A constructor is called only when the instance of the object is created. It is also used to verify that they are sufficient resources for objects to perform a specific task.
There are two types of constructors in Python, and they are:
- Parameterized constructor
- Non-parameterized constructor
Ques 85. How can we create a constructor in Python programming?
The _init_ method in Python stimulates the constructor of the class.
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
Output:
John
16
Most helpful rated by users:
- What is Python?
- How do I share global variables across modules?
- How do you set a global variable in a function in Python?
- Why can't I use an assignment in an expression?
- What are the rules for local and global variables in Python?