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

  • Home
  • SEARCH
  • 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 6989019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:09:29+00:00 2026-05-27T19:09:29+00:00

I am a newcomer to Python. I don’t understand how/why the self argument is

  • 0

I am a newcomer to Python. I don’t understand how/why the self argument is used:

class Person:
    def __init__(self, name):
        self.name = name
    def sayHi(self):
        print 'Hello, my name is', self.name

p = Person('Swaroop')
p.sayHi()

This code is from a Byte of Python. This code could have just as easily been written as:

def sayHi(self, name):
            print 'Hello, my name is', name

p = Person('Swaroop')
p.sayHi()

…right? What is the purpose of making name a field of self?

  • 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-27T19:09:29+00:00Added an answer on May 27, 2026 at 7:09 pm

    It seems you are oversimplifying a not so simple thing.
    In object-oriented programming, a Class is a Declarative Construct which gives a blueprint as to what an object (a manifestation of this blueprint) would contain (properties) and how it would behave (members).
    Every manifestation of such a Class is called an Object which has a defined and specific instance. From an object via any of this behavioral attributes called member functions/methods we need somehow to refer to that instance of the object and remember individual elements within it and make a clear distinction with the other non member entities.

    Consider Your example

    class Person:
        def __init__(self, name):
            self.name = name
        def sayHi(self):
            print 'Hello, my name is', self.name
    

    Every Instance of this Person (Tom, Dick, Harry) is unique and within each instance, to refer back to itself we need some idiom like (self in Python, this ptr is C++ or this in Java).

    So in the __init__ method when you need to demarcate between the name attribute of Person with the name parameter we can easily do so with self. Not only that, at any instance we can keep on referring back to this name via the self.

    Making an instance of Person p=Person('Swaroop') and then invoking sayHi contrasting to calling just a function sayHi which is not a part of an object has two implications

    1. The Function is transient, has no state, no instance and its like Saying this Guy “Hey Mr. who ever you are say ‘Hello, my name is Swaroop'”. It’s like on every run you have to make an imbecile make aware of his name who will forget the next moment you will meet him. It’s like this Guy is suffering from Anterograde_amnesia. Calling sayHi of the instance of Person named Swaroop on the other hand would mean something like a physically existing Swaroop greeting back his Name who has a persistent memory and would never forget unless he adopts a new name.
    2. The Function never remembers but forgets every time the call ends. The Person object will remember the name until you explicitly or implicitly Kill That Person.

    If you have a background of C++ and might be wondering why on earth do we need to add that extra parameter to the function call where as in C++ this pointer is never passed.
    Well Candidly speaking it does. If you read the C++ calling convention, whether X86 or X64, the this pointer is passed through the register ecx to the method to give an handle to itself. This is more explicit here where we deliberately pass the handle to the current instance to the method.

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

Sidebar

Related Questions

I'm newcomer in django, and here is question: I have model class: def Client(models.User)
I am a newcomer to the Python and Twisted game so excuse the ignorance
I am a newcomer to Scala. In 2.7.7, the following code abstract class C
As a newcomer to Cocoa, I am struggling to understand why the generic NSResponder
I'm still a newcomer to python, so I hope this question isn't inane. The
This is my first post and I'm still a Python and Scipy newcomer, so
newcomer and first ever question here. I am using the multiprocessing module of Python
I'm a newcomer to python. I just downloaded the 2.x version and am trying
I am a newcomer to python (also very sorry if this is a newb
I am a newcomer to ASP, and would like to experiment with it by

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.