Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6179083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:35:17+00:00 2026-05-24T00:35:17+00:00

In Python, I want to know if it is necessary to include __init__ as

  • 0

In Python, I want to know if it is necessary to include __init__ as the first method while creating a class, as in the example below:

class ExampleClass: 

    def __init__(self, some_message): 
        self.message = some_message 
        print "New Class instance created, with message:" 
        print self.message 

Also, why do we use self to call methods?
Can someone explain the use of “self” in detail?

Also, why do we use pass statement in Python?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:35:18+00:00Added an answer on May 24, 2026 at 12:35 am

    No, it isn’t necessary.

    For example.

    class A(object):
        def f():
            print 'foo'
    

    And you can of course use it, in this manner:

    a = A()
    a.f()
    

    In fact you can even define a class in this manner.

    class A:
        pass
    

    However, defining __init__ is a common practice because instances of a class usually store some sort of state information or data and the methods of the class offer a way to manipulate or do something with that state information or data. __init__ allows us to initialize this state information or data while creating an instance of the class.

    Here is a complete example.

    class BankAccount(object):
        def __init__(self, deposit):
            self.amount = deposit
    
        def withdraw(self, amount):
            self.amount -= amount
    
        def deposit(self, amount):
            self.amount += amount
    
        def balance(self):
            return self.amount
    
    # Let me create an instance of 'BankAccount' class with the initial
    # balance as $2000.
    myAccount = BankAccount(2000)
    
    # Let me check if the balance is right.
    print myAccount.balance()
    
    # Let me deposit my salary
    myAccount.deposit(10000)
    
    # Let me withdraw some money to buy dinner.
    myAccount.withdraw(15)
    
    # What's the balance left?
    print myAccount.balance()
    

    An instance of the class is always passed as the first argument to a method of the class. For example if there is class A and you have an instance a = A(), whenever you call a.foo(x, y), Python calls foo(a, x, y) of class A automatically. (Note the first argument.) By convention, we name this first argument as self.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already know Java, Objective C, C#, and some Python. I want to be
Using Python I want to be able to draw text at different angles using
I want to start using Python for small projects but the fact that a
I want to encrypt few files using python what is the best way I
I want to write a function in Python that returns different fixed values based
I want to run javascript/Python/Ruby inside my application. I have an application that creates
I want to try out SICP with Python. Can any one point to materials
I want my app to loop in python but have a way to quit.
I want to open a TCP client socket in Python. Do I have to
I want to use the macports version of python instead of the one that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.