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 8097767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:51:59+00:00 2026-06-05T21:51:59+00:00

I have a general question on the class definition and its use..THe below code

  • 0

I have a general question on the class definition and its use..THe below code from one of the book works fine but I have a general questions.

Here we have defined a class Point and creating 2 instance Point1 & Point2. When calculating the distance for point2, how can we pass the point1 object?

Isn’t point1 the point object, whereas the other_point is reprented as a variable.

Im little confused.

Code:

import math
class Point:
    def move(self, x, y):
        self.x = x
        self.y = y
    def reset(self):
        self.move(0, 0)
    def calculate_distance(self, other_point):
        print("Inside calculating distance")

        return math.sqrt(
                (self.x - other_point.x)**2 +
                (self.y - other_point.y)**2)

point1 = Point()
point2 = Point()
point1.reset()
point2.move(5,0)
print(point2.calculate_distance(point1))                    
  • 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-06-05T21:52:01+00:00Added an answer on June 5, 2026 at 9:52 pm

    That’s what the self variable does. So when you are inside the definition of a class, you can use self to identify the object whose data you are trying to manipulate.

    For example, suppose you have a class called human (which has a member variable named age), and every year, you want to increase the age of that human by calling the increment_age function. Then, you could write the following code:

    class Human:
        def __init__(self):
            self.age = 0
    
        def increment_age(self):
            self.age += 1
    
    >>> h = Human()
    >>> print h.age
    0
    >>> h.increment_age()
    >>> print h.age
    1
    

    So you see, by calling self, you are referring to the object itself. In your example, this would translate to self referring to point1.

    Now, suppose that in the Human class, we want to add a function that allows two humans to fight. In this case, one human would have to fight another human (suppose that fighting another human increases your life by one and decreases the other human’s life by one). In that case, you could write the following function within the Human class:

    def fight(self, other_human):
        self.age += 1
        other_human.age -= 1
    

    Now:

    >>> h1 = Human()
    >>> h2 = Human()
    >>> h1.age = 5
    >>> h2.age = 3
    >>> print h1.age
    5
    >>> print h2.age
    3
    >>> h1.fight(h2)
    >>> print h1.age
    6
    >>> print h2.age
    2
    

    Thus you can see in this example that h2 is the other_human in the fight function.

    Hope that helps

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

Sidebar

Related Questions

I have general question about designated initializer. I have a some class and from
I have a general question about the way that database indexing works, particularly in
I have general question regarding the use of pointers vs. references in this particular
I have a basic question in Java, but it's a general question in OOP.
I have a general question about Hibernate that I'm batteling with. I have class
I need to ask a general question. I don't have the code in front
I have a general question about jQuery. Lets say I have the following code:
I have a general OO design question that stems from a Hibernate Model. Example
I have a general question about interpreters of functional languages: Are there actually any
i have a general question : i'd like to develop a facebook application using

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.