#
Code Crafter IT Innovation
Loading...
  • Bansal Computer Center
  • punjabedu.bansalcenter.com

Python Directories and Super()

Hello
Hello

Hello

Hello

super() – Accessing Parent Class

Definition


Use

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


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


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


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

What students Say?

Here are the stdudents...

#

Contact Us

Empower your tech dreams with us. Get in touch!

  • #
  • #
  • #
  • #
  • #