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

Python Methods

Hello
Hello

Hello

Hello

What are Methods in Python Classes?

Definition

Types:

Type Instance Method Class Method Static Method Constructor
Keyword Used self cls None self
Special Symbol None @classmethod @staticmethod __init__()
Common Use Uses object data Uses class data Just a helper method, no self or cls Runs when object is created

Hello

Hello

Hello

Instance Method

Definitions

Syntax:


  class Student:
   def show(self):      # instance method
    print("This is an instance method")

  s = Student()
  s.show()  # Output: This is an instance method

Hello

Hello

Hello

Class Method

Definitions

Syntax:


class Student:
    school = "ABC School"

    @classmethod
    def show_school(cls):
        print("School name is:", cls.school)

Student.show_school()  # Output: School name is: ABC School

Hello

Hello

Static Method

Definitions

Syntax:


class Student:
    @staticmethod
    def greet():
        print("Welcome to the class!")

Student.greet()  # Output: Welcome to the class!

Hello

Hello

Constructor Method – __init__()

Definitions

Syntax:


class Student:
    def __init__(self, name):
        self.name = name

    def show_name(self):
        print("Name is:", self.name)

s = Student("Kiran")
s.show_name()  # Output: Name is: Kiran

Hello

Hello

What students Say?

Here are the stdudents...

#

Contact Us

Empower your tech dreams with us. Get in touch!

  • #
  • #
  • #
  • #
  • #