Hello
super() – Accessing Parent Class
Definition
- super() is used in inheritance to call methods or constructors from a parent class.
Use
- Inside a child class to access methods or __init__() of the parent class.
Syntax:
class Parent:
def show(self):
print("Parent method")
class Child(Parent):
def show(self):
super().show() # calls Parent's show()
print("Child method")
c = Child()
c.show()
Use in __init__():
class A:
def __init__(self):
print("Init A")
class B(A):
def __init__(self):
super().__init__()
print("Init B")
b = B()
Hello
Hello
dir() – List Attributes/Methods
Definition
- Returns a list of all valid attributes, methods, and properties of an object (including default ones).
Example 1: For a list
a = [1, 2, 3]
print(dir(a))
Example 2: For your class
class Student:
def __init__(self, name):
self.name = name
print(dir(Student))
Hello
Hello
__dict__ – Object’s Attribute Dictionary
Definition
- Shows a dictionary of only user-defined attributes of an object or class.
Example
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p = Person("Mayank", 14)
print(p.__dict__)
Output:
{'name': 'Mayank', 'age': 14}
Hello
Hello
help() – Built-in Documentation
Definition
- Provides help text or docstring of any object, class, function, module, etc.
Example on user-defined class:
class Teacher:
"""This is a sample class for a Teacher"""
def __init__(self):
"""Initializes the teacher"""
self.subject = "Math"
help(Teacher)
Hello